Dec 19 2007

How to use a “while” loop in Ruby by singing a song about beer!

If you're new here, you may want to start with my most popular posts. Then, subscribe to my RSS feed to stay updated. Thanks for visiting!

I have been going through the Ruby tutorial at Chris Pine’s Learn to Program site. While, I have been making do with Ruby on Rails, I have never officially learned Ruby yet. I wanted a beginner tutorial to get a feel for the actual Ruby language itself.

At the end of the Chapter on Flow Control, Chris asks the reader to write a program that displays the lyrics to “99 bottles of beer…” I took on this task and added a few other features to enhance the output (like pausing so the reader can read the lines as they are written on the screen). This program demonstrates several concepts like While loops, methods, and the Don’t Repeat Yourself (DRY) mantra.

If you followed at least the Ruby portion of my previous tutorial on “How to install ruby on rails on Ubuntu Gutsy Gibbon (7.10)” you should be able to run this program if you copy and paste the code below into a text document ( keep in mind both the “bottles of beer” and the “bottle of beer” text should be on one line, the “beer” portion is only looks like a separate line because of formatting), rename it beer.rb (or whatever you like) and type the following into the Terminal:
ruby beer.rb

beer = 99 #Cuz that's how many beerz you have!
pass = 'Take one down and pass it around...' #Cuz you has to share dem beerz!
# Begin da loop!
while beer > 2 #Gotta gets da gramma right. Sees it below da loop!
puts ' '
puts beer.to_s + ' bottles of beer on the wall, ' + beer.to_s + ' bottles of beer!'
puts pass
puts ' '
beer = beer - 1
sleep 5 #Gotta wait till the line is sung!
puts beer.to_s + ' bottles of beer on the wall.'
puts ' '
sleep 2 #Gotta wait till the line is sung!
#End da loop!
#Gets da gramma right with dis piece below!
beer = beer - 1
puts beer.to_s + ' bottle of beer on the wall, ' + beer.to_s + ' bottle of beer!'
puts pass
puts ' '
beer = beer - 1
puts 'You drank all my beer! You Jerk!!!' #It's a damn shame!

Have fun with beer.rb. I am sure that you can make improvements. Here are a few suggestions:

  1. Make it so after every 10 or 20 beers, the speech begins to slur
  2. Add a “hic!” between lines every 10 or 20 lines
  3. Make it so a scripted bar brawl happens after the last beer is gone

If you add these improvements, I would love to hear about them in the comments below this post. Perhaps you could even share your code snippets.

This tutorial has been great so far. I am probably going to buy Chris Pine’s Learn to Program book so I can go through the expanded version of this tutorial. Happy Coding!

Technorati Tags: , ,

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.


2 Comments on this post


  1. Ian Smith said:

    I love this!

    January 21st, 2008 at 11:28 am
  2. hstagner said:

    Hello Ian,

    Glad you enjoyed it! Thanks for reading!

    January 22nd, 2008 at 1:35 pm


Subscribe Form

Subscribe to Blog


Recent Readers

                  Computers Blogs - Blog Top Sites