Language Ruby
(object-oriented version)
Date: | 04/20/05 |
Author: | Mike Gertz |
URL: | n/a |
Comments: | 2 |
Info: | http://www.rubycentral.com |
Score: | (3.03 in 39 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
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
Using continuations, singleton classes | Victor Borja | 09/15/06 | 9 | |
minimal version | Anonymous | 05/18/05 | 3 | |
shows inheritance, iterators, yield, etc | Kian Wright | 06/10/05 | 0 | |
alternative version | Greg T. | 05/18/05 | 10 | |
In words | Daniel Straight | 07/10/06 | 1 | |
wall-based OO version | Kevin Baird | 07/07/05 | 2 | |
monkeypatch and anonymous procs | J. B. Rainsberger | 04/04/07 | 0 | |
Readably re-opening Integer, teetotaller | Eric Budd | 01/06/08 | 0 |
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!
Comments
Gerardo Garza said on 01/15/07 01:12:28
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
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.