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!’
5 responses so far ↓
1
Chantal
// May 17, 2008 at 8:07 pm
Well, that was odd. Wordpress decided that it wanted to ignore the code tags I put in. Should be fine now.
2
Willian Molinari
// May 17, 2008 at 8:20 pm
Æ!!
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
3
Chantal
// May 17, 2008 at 8:47 pm
Your code is even shorter. I like.
4
Willian Molinari
// May 19, 2008 at 8:38 am
Æ!!
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
5
Chantal
// May 20, 2008 at 12:57 am
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.
Leave a Comment