Language Ruby
(shows inheritance, iterators, yield, etc)
Date: | 06/10/05 |
Author: | Kian Wright |
URL: | n/a |
Comments: | 0 |
Info: | n/a |
Score: | (3.00 in 28 votes) |
# 99 bottles of beer, in Ruby # illustrates inheritance, blocks/yield, class methods, arrays, iterators # Kian Wright class Song def Song.sing(fridge) sing_how_many(fridge, :on_the_wall) sing_how_many(fridge, :nowhere) yield sing_how_many(fridge, :on_the_wall) puts end def Song.sing_how_many(fridge, where=:on_the_wall) puts "#{fridge.to_s} of beer #{where == :on_the_wall ? 'on the wall' : ''}" end end class Beer def drink(from=nil) from.delete(self) if from && from.respond_to?(:delete) puts "take one down and pass it around" end end class Beers < Array def drink(how_many=length) how_many = length if how_many == :all self[0,how_many].each do |beer| Song.sing(self) { beer.drink(self) } end end def restock(how_many) Song.sing(self) { buy(how_many) } end def buy(how_many) fill(0, how_many) { Beer.new } puts "go to the store and buy some more" end def to_s case length when 0 "no more bottles" when 1 "one bottle" else "#{length} bottles" end end end fridge = Beers.new(99) {|i| Beer.new } fridge.drink(:all) fridge.restock(99)
Download Source | Write Comment
Alternative Versions
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
Using continuations, singleton classes | Victor Borja | 09/15/06 | 9 | |
object-oriented version | Mike Gertz | 04/20/05 | 2 | |
minimal version | Anonymous | 05/18/05 | 3 | |
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