Created
October 17, 2017 23:48
-
-
Save joshforbes/495f3718c03d881edf53fbb0e6ed0dfa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bottles | |
def song | |
verses(99, 0) | |
end | |
def verses(first, last) | |
first.downto(last).map { |n| verse(n) }.join("\n") | |
end | |
def verse(n) | |
VerseFactory.make(n).call | |
end | |
end | |
class VerseFactory | |
def self.make(n) | |
return RunningOutVerse.new if n == 2 | |
return LastOneVerse.new if n == 1 | |
return AllOutVerse.new if n == 0 | |
StandardVerse.new(n) | |
end | |
end | |
class StandardVerse | |
def initialize(n) | |
@n = n | |
end | |
def call | |
<<-VERSE | |
#{@n} bottles of beer on the wall, #{@n} bottles of beer. | |
Take one down and pass it around, #{@n-1} bottles of beer on the wall. | |
VERSE | |
end | |
end | |
class RunningOutVerse | |
def call | |
<<-VERSE | |
2 bottles of beer on the wall, 2 bottles of beer. | |
Take one down and pass it around, 1 bottle of beer on the wall. | |
VERSE | |
end | |
end | |
class LastOneVerse | |
def call | |
<<-VERSE | |
1 bottle of beer on the wall, 1 bottle of beer. | |
Take it down and pass it around, no more bottles of beer on the wall. | |
VERSE | |
end | |
end | |
class AllOutVerse | |
def call | |
<<-VERSE | |
No more bottles of beer on the wall, no more bottles of beer. | |
Go to the store and buy some more, 99 bottles of beer on the wall. | |
VERSE | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment