Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Bottle
- def self.verse(bottles)
- new(bottles).song
- end
- attr_reader :bottles
- def initialize(bottles)
- @bottles = bottles
- end
- def song
- [bottles_message, "\n", take_down_message, "\n"].join
- end
- def bottles_message
- "#{context_bottle(bottles, empty: 'No more bottles')} of beer on the wall, #{context_bottle(bottles)} of beer."
- end
- def take_down_message
- return buy_more_message if bottles == 0
- @bottles -= 1
- "#{context_down} and pass it around, #{context_bottle(bottles)} of beer on the wall."
- end
- def context_bottle(amount, empty: 'no more bottles')
- if amount > 1
- "#{amount} bottles"
- elsif amount == 1
- "#{amount} bottle"
- else
- empty
- end
- end
- def context_down
- bottles > 0 ? "Take one down" : "Take it down"
- end
- def buy_more_message
- "Go to the store and buy some more, 99 bottles of beer on the wall."
- end
- end
- class Bottles
- def verse(num)
- Bottle.verse(num)
- end
- def verses(top, bottom)
- top.downto(bottom).map { |current| verse(current) }.join("\n")
- end
- def song
- verses(99, 0)
- end
- end
Add Comment
Please, Sign In to add comment