Created
March 18, 2023 16:04
-
-
Save aspencer8111/5a87c8c5e5dfcccd6e8a54cd5c1c8685 to your computer and use it in GitHub Desktop.
99 Bottles of OOP - Pre-read attempt
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
#!/usr/bin/ruby | |
class BottlesSong | |
def initialize(number_of_bottles) | |
@number_of_bottles = number_of_bottles | |
end | |
def sing | |
bottles.each do |number_of_bottles| | |
if number_of_bottles == 1 | |
singularized_verse(number_of_bottles) | |
else | |
pluralized_verse(number_of_bottles) | |
end | |
end | |
end | |
private | |
attr_reader :number_of_bottles | |
def bottles | |
(1..@number_of_bottles).to_a.reverse | |
end | |
def pluralized_top_verse(number_of_bottles) | |
2.times { puts "#{number_of_bottles} bottles of beer on the wall" } | |
end | |
def singularized_top_verse(number_of_bottles) | |
2.times { puts "#{number_of_bottles} bottle of beer on the wall" } | |
end | |
def pluralized_bottom_verse(number_of_bottles) | |
puts "You take one down, pass it around, #{number_of_bottles - 1} bottles of beer on the wall" | |
end | |
def singularized_bottom_verse(number_of_bottles) | |
puts "You take one down, pass it around, no more bottles of beer on the wall!" | |
end | |
def pluralized_verse(number_of_bottles) | |
pluralized_top_verse(number_of_bottles) | |
pluralized_bottom_verse(number_of_bottles) | |
spacer | |
end | |
def singularized_verse(number_of_bottles) | |
singularized_top_verse(number_of_bottles) | |
singularized_bottom_verse(number_of_bottles) | |
end | |
def spacer | |
puts "" | |
end | |
end | |
BottlesSong.new(99).sing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: