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: |
# 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
| 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 | |
| object-oriented version | Mike Gertz | 04/20/05 | 2 | |
| wall-based OO version | Kevin Baird | 07/07/05 | 2 | |
| 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