I'm working through the TestFirst.org tutorials and have gotten an error message I can't untangle:
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)>'
Here is the code those errors are talking about ("def repeat" is line 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
And here is the rake test that set it off:
it "should repeat a number of times" do
repeat("hello", 3).should == "hello hello hello"
end
I understand that the error message is about trying to use a string like a numeric value but I can't see how or where that is happening
The below is the problem source
In the line
repetition + " " + say
, you are trying to do a concatenation between aFixnum
andString
instance, which caused the error String can't be coerced into Fixnum.Your code can be written as :
In my test_spec.rb file :-
Lets run the test :-
update
If you think,
repetition
declared outside of the block and inside the block are same, you are completely wrong. They are different, as they created in 2 different scopes. See the below example :-