Title / Description
Code class Bottles def song verses(99, 0) end def verses(starting, ending) starting.downto(ending).collect {|i| verse(i)}.join("\n") end def verse(number) bottle_number = BottleNumber.for(number) "#{bottle_number} of milk on the wall, ".capitalize + "#{bottle_number} of milk.\n" + "#{bottle_number.action}, " + "#{bottle_number.successor} of milk on the wall.\n" end end class BottleNumber def self.for(number) case number when 0 BottleNumber0 when 1 BottleNumber1 when 6 BottleNumber6 else begin const_get("BottleNumber#{number}") rescue NameError BottleNumber end.new(number) end attr_reader :number def initialize(number) @number = number end def to_s "#{quantity} #{container}" end def container "bottles" end def quantity number.to_s end def action "Take #{pronoun} down and pass it around" end def pronoun "one" end def successor BottleNumber.for(number - 1) end end class BottleNumber0 < BottleNumber def quantity "no more" end def action "Go to the store and buy some more" end def successor BottleNumber.for(99) end end class BottleNumber1 < BottleNumber def container "bottle" end def pronoun "it" end end class BottleNumber6 < BottleNumber def quantity "1" end def container "six-pack" end end
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code