Guest User

Untitled

a guest
Jun 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. class Bottle
  2. def self.verse(bottles)
  3. new(bottles).song
  4. end
  5.  
  6. attr_reader :bottles
  7.  
  8. def initialize(bottles)
  9. @bottles = bottles
  10. end
  11.  
  12. def song
  13. [bottles_message, "\n", take_down_message, "\n"].join
  14. end
  15.  
  16. def bottles_message
  17. "#{context_bottle(bottles, empty: 'No more bottles')} of beer on the wall, #{context_bottle(bottles)} of beer."
  18. end
  19.  
  20. def take_down_message
  21. return buy_more_message if bottles == 0
  22.  
  23. @bottles -= 1
  24. "#{context_down} and pass it around, #{context_bottle(bottles)} of beer on the wall."
  25. end
  26.  
  27. def context_bottle(amount, empty: 'no more bottles')
  28. if amount > 1
  29. "#{amount} bottles"
  30. elsif amount == 1
  31. "#{amount} bottle"
  32. else
  33. empty
  34. end
  35. end
  36.  
  37. def context_down
  38. bottles > 0 ? "Take one down" : "Take it down"
  39. end
  40.  
  41. def buy_more_message
  42. "Go to the store and buy some more, 99 bottles of beer on the wall."
  43. end
  44. end
  45.  
  46. class Bottles
  47. def verse(num)
  48. Bottle.verse(num)
  49. end
  50.  
  51. def verses(top, bottom)
  52. top.downto(bottom).map { |current| verse(current) }.join("\n")
  53. end
  54.  
  55. def song
  56. verses(99, 0)
  57. end
  58. end
Add Comment
Please, Sign In to add comment