Created
December 13, 2017 19:34
-
-
Save LolWalid/de795af6d787e4aeac086263a40c5747 to your computer and use it in GitHub Desktop.
Solve Ruby Woskhop BeerSong
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
class BeerSong | |
def verse(nb_bottle) | |
s = "#{pluralize(nb_bottle, capitalize: true)} of beer on the wall, #{pluralize(nb_bottle)} of beer.\n" | |
s + if nb_bottle == 0 | |
"Go to the store and buy some more, 99 bottles of beer on the wall.\n" | |
else | |
"Take #{it_or_one(nb_bottle - 1)} down and pass it around, #{pluralize(nb_bottle - 1)} of beer on the wall.\n" | |
end | |
end | |
def verses(nb_bottle1, nb_bottle2) | |
s = "" | |
for i in nb_bottle2..nb_bottle1 | |
if i == nb_bottle2 | |
s = verse(i) + s | |
else | |
s = verse(i) + "\n" + s | |
end | |
end | |
s | |
end | |
def lyrics | |
verses(99, 0) | |
end | |
private | |
def pluralize(nb_bottle, capitalize: false) | |
if nb_bottle == 1 | |
"#{nb_bottle} bottle" | |
elsif nb_bottle == 0 | |
capitalize ? "no more bottles".capitalize : "no more bottles" | |
else | |
"#{nb_bottle} bottles" | |
end | |
end | |
def it_or_one(nb_bottle_left) | |
if nb_bottle_left == 0 | |
'it' | |
else | |
'one' | |
end | |
end | |
end | |
module BookKeeping | |
VERSION = 2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment