I am attempting a "99 Bottles" program. I tried to simplify it, but I got "string cant be coerced into a Fixnum":
num_at_start = 99
num_now = num_at_start
bobo = " bottles of beer on the wall"
bob = " bottles of beer!"
while num_now > 2
puts num_now.to_s + bobo.to_s
puts num_now.to_s + bob.to_s
puts num_at_start.to_i - 1 + bobo.to_s
gets
end
The problem is here:
Ruby suggests types of resulting expressions as args come to interpreter, from left to right. Here you attempt to sum two integers, making the result to be an integer.
Fixnum#+
requires instance ofFixnum
as an operand, but therebobo.to_s
, which isString
, comes.You should use inplace eval here:
The whole
while
loop should be actually written as:BTW, there is another problem: an endless loop; but it’s up to you to fix this error after you got the code you have now to work.