应该重复多次

编程入门 行业动态 更新时间:2024-10-24 14:18:34
本文介绍了应该重复多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是 simon_says_spec.rb

Here is simon_says_spec.rb

require simon_says describe "repeat" do it "should repeat" do repeat("hello").should == "hello hello" end # Wait a second! How can you make the "repeat" method # take one *or* two arguments? # # Hint: *default values* it "should repeat a number of times" do repeat("hello", 3).should == "hello hello hello" end end end

这是我的 simon_says.rb

And here is my simon_says.rb

def repeat(x,y) y.times do print x + ‘ ‘ end end

在我修复 def repeat(x,y = 2) 之后,当我在窗口上运行 rake 时,它工作得很好.但是当我在 mac 上运行 rake 时.这是我得到的

After i fixed def repeat(x,y = 2) When I run rake on window, it works just fine. But when i run rake on mac. This is what i got

/Users/thanhnguyen/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': /Users/thanhnguyen/Downloads/learn_ruby-master/03_simon_says/simon_says.rb:10: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '(' (SyntaxError) /Users/thanhnguyen/Downloads/learn_ruby-master/03_simon_says/simon_says.rb:11: syntax error, unexpected end-of-input, expecting keyword_end from /Users/thanhnguyen/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /Users/thanhnguyen/Downloads/learn_ruby-master/03_simon_says/simon_says_spec.rb:14:in `<top (required)>' from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load' from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files' from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each' from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files' from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run' from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run' from /Users/thanhnguyen/.rvm/gems/ruby-2.1.1/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun' /Users/thanhnguyen/.rvm/rubies/ruby-2.1.1/bin/ruby -S rspec /Users/thanhnguyen/Downloads/learn_ruby-master/03_simon_says/simon_says_spec.rb -I/Users/thanhnguyen/Downloads/learn_ruby-master/03_simon_says -I/Users/thanhnguyen/Downloads/learn_ruby-master/03_simon_says/solution -f documentation -r ./rspec_config failed

我已经为这个错误苦苦挣扎了几天,但仍然无法修复它.我跑

Ive been struggling with this error for couple days but still cannot fix it. I run

`gem install spec`

我运行捆绑安装

这是我的系统

ruby 2.1.1 rails 4.1.1 rvm 1.25.25 bundler 1.6

有人可以帮忙吗?

推荐答案

在下面的部分中,您只传递了 1 参数而不是 2.

In the below part, you passed only 1 argument instead 2.

describe "repeat" do it "should repeat" do repeat("hello").should == "hello hello" # ^ why only 1 argument ? end

根据你的代码,它应该是 repeat("hello", 2).should == "hello hello".

As per your code, it should be repeat("hello", 2).should == "hello hello".

根据评论提示,你也可以写:-

def repeat(x, y = 2) y.times { print x + ‘ ‘ } end

现在,您编写的测试代码将可以正常工作,并且具有上述修改后的方法定义.

Now, the test code you wrote, will work without error, with above modified method definition.

require simon_says describe "repeat" do it "should repeat" do # here you are not passsing the second argument. But *repeat* method has `2` # as its default value of the second argument. So no issue will be here. repeat("hello").should == "hello hello" end it "should repeat a number of times" do repeat("hello", 3).should == "hello hello hello" end end

阅读此默认参数以了解,默认参数如何在 Ruby 中工作.

Read this default argument to know, how default argument works in Ruby.

更多推荐

应该重复多次

本文发布于:2023-11-22 20:56:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!