Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Ruby

(object-oriented version)

Date:04/20/05
Author:Mike Gertz
URL:n/a
Comments:2
Info:http://www.rubycentral.com
Score: (2.97 in 30 votes)
#99 Bottles of beer, in Ruby
#by Mike Gertz, Jan 5, 2003
#See www.rubycentral.com for info on the language.

class OneBottle
     def sing
	puts "One bottle of beer on the wall."
	puts "One bottle of beer."
	puts "Take it down, pass it around."
	puts "No more bottles of appear on the wall!"
     end
end

class TwoBottles
     def sing
	puts "Two bottles of beer on the wall."
	puts "Two bottles of beer."
	puts "Take one down, pass it around."
	puts "One bottle appears on the wall."
	puts 
     end
end

class Bottles
    attr_reader :number

    def Bottles.verse( number )
	if number > 2
	    Bottles.new( number )
        elsif number == 2
	    TwoBottles.new
	else
	    OneBottle.new
        end
    end

    def initialize( number )
        @number = number
    end

    def sing
	puts "#{number} bottles of beer on the wall."
	puts "#{number} bottles of beer."
	puts "Take one down, pass it around,"
	puts "#{number - 1} bottles appear on the wall!"
	puts
    end
end

99.downto(1) do | i |
    Bottles.verse(i).sing
end

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Using continuations, singleton classesVictor Borja09/15/069
alternative versionGreg T.05/18/0510
minimal versionAnonymous05/18/053
shows inheritance, iterators, yield, etcKian Wright06/10/050
In wordsDaniel Straight07/10/061
wall-based OO versionKevin Baird07/07/052
monkeypatch and anonymous procsJ. B. Rainsberger04/04/070
Readably re-opening Integer, teetotallerEric Budd01/06/080

Comments

>>  Gerardo Garza said on 01/15/07 01:12:28

Gerardo Garza Here is an OO version I wrote last year after reading the "Ruby in Twenty Minutes" tutorial at a Ruby language website (www.ruby-lang.org) and doing some further research into syntax. GG

-------------------------
#!/usr/bin/ruby

# 99 Bottles of Beer in Ruby (OO version)
# by Gerardo Garza, 2006

class Bottle
attr_accessor :count

def initialize (count=99)
@count = count
@start = count
@old = count
end

def how_many
if (@count > 1)
output = "#{@count.to_s} bottles of beer"
elsif (@count == 1)
output = "#{@count.to_s} bottle of beer"
else
output = "No bottles of beer"
end

if (@count == @old)
puts "#{output} on the wall. #{output}."
else
puts "#{output} on the wall."
@old = @count
end
end

def take_down
if (@count == 0)
print "Go to the store, buy some more. "
@count = @start
else
print "Take one down, pass it around. "
@count -= 1
end
end
end

killians = Bottle.new

killians.count.downto(0) do
puts
killians.how_many
killians.take_down
killians.how_many
end

>>  Devlin said on 07/12/08 06:11:22

Devlin class OneBottle
def sing
puts "One bottle of beer on the wall."
puts "One bottle of beer."
puts "Take it down, pass it around."
puts "No more bottles of appear on the wall!"
end
end

Couldn't that have just been a function instead of a function within a class? Just a bit excessive.

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: