Chantal’s Blog

This is where my brain lives.

Chantal’s Blog header image 2

99 Bottles of Beer on the Wall – Ruby Code

May 17th, 2008 by Chantal

Here is my (very simple) version of 99 Bottles of Beer on the Wall in Ruby. I the only fancy thing I am doing is “while”. I don’t like the fact that the song is duplicated to handle one bottle, so I will eventually change that. I have a couple of ideas, but I am not sure which I want to use.

#99 Bottles of Beer on the Wall

bottles = 99

while bottles != 1
puts "#{bottles} bottles of beer on the wall, #{bottles} bottles of beer."
puts "Take one down and pass it around, #{bottles -1} bottles of beer on the wall."
puts ''
bottles = bottles - 1
end

# Do this when there is one bottle left so we don't have "one bottles" of beer
puts "#{bottles} bottle of beer on the wall, #{bottles} bottle of beer."
puts "Take one down and pass it around, no bottles of beer on the wall."
puts ''
puts 'Time to buy some more beer!'

Tags:   · 5 Comments

Leave a Comment

5 responses so far ↓

  • Well, that was odd. Wordpress decided that it wanted to ignore the code tags I put in. Should be fine now.

  • Æ!!

    Hi Chantal!
    I like it! =)

    I made a little joke too:

    total_of_bottles = remaining_bottles = 99
    total_of_bottles.times do |total_of_bottles|
    puts “#{remaining_bottles-total_of_bottles} bottle of beer on the wall, #{remaining_bottles-total_of_bottles} bottle of beer.\nTake one down and pass it around, no bottles of beer on the wall.\n\n”
    end
    puts “Time to buy some more beer!”

    Keep joking with codes, is so fun and you’ll learn so much! I’ll keep my eyes on your twitter! =)

    Cheers,

    Willian Molinari

  • Your code is even shorter. I like.

  • Æ!!

    Hi Chantal!
    Another one with downto method:

    bottles = 99
    bottles.downto(1) do |bottles|
    puts “#{bottles} bottle of beer on the wall, #{bottles} bottle of beer.\nTake one down and pass it around, no bottles of beer on the wall.\n\n”
    end
    puts “Time to buy some more beer!”

    =)

    Cheers,

    Willian Molinari

  • I like that one too, but the output isn’t quite right. It says:

    99 bottle of beer on the wall, 99 bottle of beer.
    Take one down and pass it around, no bottles of beer on the wall.

    It should say “bottles” and it should say that there are 98 left.