Skip to content

Instantly share code, notes, and snippets.

@octosteve
Forked from danvideo/bottlesofbeer.rb
Last active December 27, 2015 02:39
Show Gist options
  • Save octosteve/7253815 to your computer and use it in GitHub Desktop.
Save octosteve/7253815 to your computer and use it in GitHub Desktop.
bottle_lyric = ->(count) { "#{count} #{count == 1 ? "bottle" : "bottles"} of beer" }
99.downto(2) do |c|
puts "#{bottle_lyric.call(c)} on the wall,"
puts "#{bottle_lyric.call(c)}!"
puts "Take one down, pass it around"
puts "#{bottle_lyric.call(c-1)} on the wall!"
puts
end
puts "1 bottle of beer on the wall,"
puts "1 bottle of beer!"
puts "Take one down, pass it around,"
puts "No more bottles of beer on the wall :-("
class BottlesOfBeer
attr_reader :bottles_in_the_bar
def initialize(bottles_in_the_bar)
@bottles_in_the_bar = bottles_in_the_bar
end
def count_off
bottles_in_the_bar.downto(2) do |number|
current_beer_count = beer_count_lyric(number)
next_beer_count = beer_count_lyric(number - 1)
puts "#{current_beer_count} on the wall,
#{current_beer_count}!
Take one down, pass it around,
#{next_beer_count} on the wall!"
end
sing_final!
end
def sing_final!
puts "1 bottle of beer on the wall,
1 bottle of beer!
Take one down, pass it around,
No more bottles of beer on the wall :-("
end
private
def beer_count_lyric(beer_bottle_count)
"#{pluralize('bottle', beer_bottle_count)} of beer"
end
def pluralize(word, count)
"#{count} #{word}".tap do |p|
p << "s" if count != 1
end
end
end
puts "How many bottles of beer have you got? "
bottles_in_the_bar = gets.to_i
bottle = BottlesOfBeer.new(bottles_in_the_bar)
bottle.count_off
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment