应重复多次

时间:2014-05-15 03:01:56

标签: ruby-on-rails ruby

这是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

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

我修复了def repeat(x,y = 2)当我在窗口上运行rake时,它运行正常。但是当我在mac上运行rake时。这就是我得到的

/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

我几天来一直在努力解决这个错误,但仍无法修复它。我跑

`gem install spec` 

我运行bundle install

这是我的系统

ruby 2.1.1
rails 4.1.1
rvm 1.25.25
bundler 1.6

有人会帮忙吗?

3 个答案:

答案 0 :(得分:3)

捡起@Arup离开的地方(你已经通过跟进编辑了这个问题)

  1. 您使用的是非ASCII字符而不是' - ruby​​不喜欢这样。
  2. 您的代码不会返回链接文本,而是您将其相乘的次数(它会打印结果)

    3.times do '123' end
    # => 3
    

    所以你的测试会失败 更好的方法是

    y.times.map { x + ' ' }.join
    
  3. 以更红宝石的方式:

    ([x] * y).join(' ')
    

答案 1 :(得分:1)

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

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

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

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

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

现在,您编写的测试代码将无误地工作,具有上述修改的方法定义。

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

阅读此default argument以了解默认参数如何在Ruby中运行。

答案 2 :(得分:0)

您缺少do循环的结束语句。

def repeat(x, y = 2)
    y.times do print x + ' ' end
end
相关问题