Joey Schulz Joey Schulz - 30 days ago 10
Ruby Question

Trouble looping string with 99 bottles of beer

I was able to count down to 97 bottle of beers but I am having trouble looping to count down to 1. Is it possible to create a loop with what I wrote? Here's what I have so far.

all_beers = (99).to_s
one_less = ((all_beers).to_i - 1).to_s
puts '' +
all_beers + ' bottles of beer on the wall, ' +
all_beers + ' bottles of beer. You take one down you pass it around ' +
one_less + ', beers on the wall!'

all_beers = one_less
one_less = ((all_beers).to_i - 1).to_s
puts '' +
all_beers + ' bottles of beer on the wall, ' +
all_beers + ' bottles of beer. You take one down you pass it around ' +
one_less + ', beers on the wall!'

Answer Source

use downto :

it will loop from the number you want upto the number you wish.

99.downto(1).each do |s|
  all_beers = s
  one_less = s - 1
  puts '' +
      all_beers.to_s + ' bottles of beer on the wall, ' +
      all_beers.to_s + ' bottles of beer. You take one down you pass it around ' +
      one_less.to_s + ', beers on the wall!'
end