r/learnruby • u/six-speed • Aug 26 '15
99 bottles of beer program throwing weird error
Hi everyone, Just started learning ruby. I am trying to rewrite a python script for the 99 bottles of beer on the wall song in Ruby, and I am hitting a weird error that I don't understand. The program loops properly until it hits 1. Here's the Ruby code:
bottles = 10
for i in bottles.downto(1) do
if i > 1
puts(i.to_s + " bottles of beer on the wall," + i.to_s + " bottles of beer, take one down, pass it around," + (i-1).to_s + " bottles of beer on the wall.")
elsif i == 1
puts(i.to_s + " bottle of beer on the wall", + i.to_s + " bottle of beer, take one down, pass it around," + (i-1).to_s + " bottles of beer on the wall.")
else
puts("no more bottles of beer on the wall, no more bottles of beer, you go to the store, buy some more," + bottles.to_s + "bottles of beer on the wall.")
end
endwhich throws this error:
ninety_nine_bottles.rb:7:in block in <main>': undefined method+@' for "1":String (NoMethodError) from ninety_nine_bottles.rb:3:in downto' from ninety_nine_bottles.rb:3:ineach' from ninety_nine_bottles.rb:3:in `<main>'
For reference, here's my original python code that I'm trying to rewrite, which works fine:
bottles = 10
for i in range(bottles,-1,-1):
if i > 1:
print("{} bottles of beer on the wall, {} bottles of beer, take one down, pass it around, {} bottles of beer on the wall.".format(i, i, i-1))
elif i == 1:
print("{} bottle of beer on the wall, {} bottle of beer, take one down, pass it around, {} bottles of beer on the wall.".format(i, i, i-1))
else:
print("no more bottles of beer on the wall, no more bottles of beer, you go to the store, get some more, {} bottles of beer on the wall".format(bottles))Thanks for your help!
4
u/quickreply100 Aug 26 '15 edited Aug 26 '15
The issue is the comma outside of the string in your 2nd
puts. You can reproduce the same error by doing this:Also, I know this is unsolicited but if you want to do the true equivalent of python's
you can just do this:
The code inside the
#{}is evaluated and converted to string for you so you can do complex stuff, e.g.:edit, even more idiomatic Ruby:
1
u/victorioushack Aug 27 '15
I saw the title of this thread and decided to see if I could write this one with my little knowledge so far. It works, but is it "best practice"? Or am I being too simple?