这是 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.
更多推荐
应该重复多次
发布评论