Skip to content

Instantly share code, notes, and snippets.

@yarmand
Created July 2, 2014 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yarmand/8c4258949a7cca5a936a to your computer and use it in GitHub Desktop.
Save yarmand/8c4258949a7cca5a936a to your computer and use it in GitHub Desktop.
SPA_2014
class Bottles
def song
verses(99, 0)
end
def verses(upper_bound, lower_bound)
upper_bound.downto(lower_bound).map { |i| verse(i) }.join("\n")
end
def verse(verse_number)
current_bn = create_bottle_number(verse_number)
next_bn = create_bottle_number(current_bn.next)
"#{current_bn.quantity.capitalize} #{current_bn.container} of beer on the wall, " +
"#{current_bn.quantity} #{current_bn.container} of beer.\n" +
"#{current_bn.action}, " +
"#{next_bn.quantity} #{next_bn.container} of beer on the wall.\n"
end
def create_bottle_number(number)
if number == 0
BottleNumber0.new(number)
elsif number == 1
BottleNumber1.new(number)
elsif number == 6
BottleSixPack.new(number)
else
BottleNumber.new(number)
end
end
end
class BottleNumber
attr_reader :number
def initialize(number)
@number = number
end
def container
'bottles'
end
def pronoun
'one'
end
def quantity
number.to_s
end
def action
"Take #{pronoun} down and pass it around"
end
def next
number - 1
end
end
class BottleSixPack < BottleNumber
def quantity
(number / 6).to_s
end
def container
'six-pack'
end
end
class BottleNumber1 < BottleNumber
def pronoun
'it'
end
def container
'bottle'
end
end
class BottleNumber0 < BottleNumber
def next
99
end
def action
"Go to the store and buy some more"
end
def quantity
'no more'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment