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

(monkeypatch and anonymous procs)

Date:04/04/07
Author:J. B. Rainsberger
URL:http://www.jbrains.ca
Comments:0
Info:ruby-central.org
Score: (2.91 in 11 votes)
# 99 bottles of beer, monkeypatch plus anonymous procs.
# (c) J. B. Rainsberger 2007 All rights reserved, but I can be bribed.

class Fixnum
  BOTTLES_FACTORY = Hash.new { |hash, number|
    lambda {
      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
    }
  }.merge( {
    1 => lambda {
      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!"
    },
    2 => lambda {
      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
    }
  })

  def sing_bottles_verse
    BOTTLES_FACTORY[self].call
  end
end

99.downto(1) { |i| i.sing_bottles_verse }

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Using continuations, singleton classesVictor Borja09/15/069
object-oriented versionMike Gertz04/20/052
minimal versionAnonymous05/18/053
shows inheritance, iterators, yield, etcKian Wright06/10/050
alternative versionGreg T.05/18/0510
In wordsDaniel Straight07/10/061
wall-based OO versionKevin Baird07/07/052
Readably re-opening Integer, teetotallerEric Budd01/06/080

Comments

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: