我正在阅读 TestFirst.org 教程并收到一条我无法解开的错误消息:
Failure/Error: repeat("hello").should == "hello hello"
TypeError:
String can't be coerced into Fixnum
# ./03_simon_says/simon_says.rb:13:in `+'
# ./03_simon_says/simon_says.rb:13:in `block in repeat'
# ./03_simon_says/simon_says.rb:12:in `times'
# ./03_simon_says/simon_says.rb:12:in `repeat'
# ./03_simon_says/simon_says_spec.rb:39:in `block (3 levels) in <top (required)>'
这是那些错误正在谈论的代码(“def repeat”是第 09 行)
def repeat (say, how_many=2)
repetition = say
how_many = how_many-1
how_many.times do |repetition|
repetition = repetition + " " + say
end
return repetition
end
这是启动它的 rake 测试:
it "should repeat a number of times" do
repeat("hello", 3).should == "hello hello hello"
end
我知道错误消息是关于尝试使用像数值这样的字符串,但我看不到发生的方式或位置