Skip to content

Instantly share code, notes, and snippets.

@rchatley
Created July 2, 2014 10:11
Show Gist options
  • Save rchatley/f968c43da5a0e69f4d0c to your computer and use it in GitHub Desktop.
Save rchatley/f968c43da5a0e69f4d0c to your computer and use it in GitHub Desktop.
class Bottles
def initialize
@verses = Hash.new { |h, num_bottles| Verse.new(num_bottles) }
@verses[6] = SixPackVerse.new(6)
@verses[7] = SixPackVerse.new(7)
end
def song
verses(99,0)
end
def verses(from, to)
from.downto(to).map { |v| verse(v) }.join("\n")
end
def verse(number_of_bottles)
@verses[number_of_bottles].first_line + "\n" + @verses[number_of_bottles].second_line + "\n"
end
class Verse
def initialize(number_of_bottles)
@number_of_bottles = number_of_bottles
end
def first_line
"#{format_bottles(@number_of_bottles)} of beer on the wall, #{format_bottles(@number_of_bottles)} of beer.".capitalize
end
def second_line
if (@number_of_bottles == 0)
"Go to the store and buy some more, 99 bottles of beer on the wall."
else
"Take #{one_or_it} down and pass it around, #{format_bottles(@number_of_bottles - 1)} of beer on the wall."
end
end
def one_or_it
if (@number_of_bottles == 1)
"it"
else
"one"
end
end
def format_bottles(n)
if (n == 1)
"1 bottle"
elsif (n == 0)
"no more bottles"
else
"#{n} bottles"
end
end
end
class SixPackVerse < Verse
def format_bottles(n)
if (n == 6)
"1 six-pack"
else
"#{n} bottles"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment