Skip to content

Instantly share code, notes, and snippets.

@bgreg
Created October 30, 2014 19:37
Show Gist options
  • Save bgreg/094b854f53b88f17a2a4 to your computer and use it in GitHub Desktop.
Save bgreg/094b854f53b88f17a2a4 to your computer and use it in GitHub Desktop.
class Bottles
def song
verses(99, 0)
end
def verses(start, finish)
start.downto(finish).map {|i| verse(i)}.join("\n")
end
def verse(number)
count = BottleCount.new(number)
next_count = BottleCount.new(count.remainder)
"#{count.name.capitalize} #{count.unit} of beer on the wall,"\
" #{count.name} #{count.unit} of beer.\n#{count.action}, "\
"#{next_count.name} #{next_count.unit} of beer on the wall.\n"
end
end
class BottleCount
attr_reader :count
def initialize(count)
@count = count
end
def action
if count == 0
"Go to the store and buy some more"
else
"Take #{pronoun} down and pass it around"
end
end
def name
if count == 0
"no more"
else
count.to_s
end
end
def pronoun
if count == 1
'it'
else
'one'
end
end
def remainder
if count == 0
99
else
count - 1
end
end
def unit
if count == 1
'bottle'
else
"bottles"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment