Last active
February 18, 2017 20:09
-
-
Save psparrow/fd34cfb4a7989db2016047d62ab02162 to your computer and use it in GitHub Desktop.
My solution to Sandi Metz and Katrina Owen's 99 Bottles of OOP problem
This file contains 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 high, low | |
high.downto(low).map { |count| verse count }.join "\n" | |
end | |
def verse count | |
bottles = Hash.new { |hash, key| "#{key} bottles" } | |
bottles[6] = '1 six-pack' | |
bottles[1] = '1 bottle' | |
bottles[0] = 'no more bottles' | |
actions = Hash.new 'Take one down and pass it around' | |
actions[1] = 'Take it down and pass it around' | |
actions[0] = 'Go to the store and buy some more' | |
remaining = count > 0 ? count - 1 : 99 | |
<<-VERSE | |
#{bottles[count].capitalize} of beer on the wall, #{bottles[count]} of beer. | |
#{actions[count]}, #{bottles[remaining]} of beer on the wall. | |
VERSE | |
end | |
end |
Updating this with a minimal refactoring and the addition of the six-pack feature.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replacing "6 bottles" with "1 six-pack" only requires adding a single line of code...