#3 The 99 Bottles of Beer Song (99bottles.rb)

This script demonstrates basic Object Orientation by singing (okay, printing) the “99 Bottles of Beer” song. The content of the example may be a bit contrived, but the program itself reveals a great deal about naming conventions in Ruby. We’ll be defining a Wall, on which there are bottles, the number of which repeatedly drops by one.

Here’s the code. Classes are the basic building blocks in Ruby, so it’s worthwhile for anyone curious about the language to understand them in some depth. We’ve already seen some built-in classes (String, Integer, and Array), so they’re not a fundamentally new concept for you at this point. What is new is the ability to define completely novel classes of your own, as we ...

Get Ruby by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.