diff --git a/bottles/lib/bottles.rb b/bottles/lib/bottles.rb index 05185b1..a5f3aa9 100644 --- a/bottles/lib/bottles.rb +++ b/bottles/lib/bottles.rb @@ -1,2 +1,39 @@ class Bottles + def verse(n) + "#{prefix(n)}\n#{suffix(n)}\n" + end + + def prefix(n) + if n > 0 + "#{numberBottles(n)} of beer on the wall, #{numberBottles(n)} of beer." + else + "No more bottles of beer on the wall, no more bottles of beer." + end + end + + def suffix(n) + if n == 0 + "Go to the store and buy some more, 99 bottles of beer on the wall." + elsif n == 1 + "Take it down and pass it around, no more bottles of beer on the wall." + else + "Take one down and pass it around, #{numberBottles(n - 1)} of beer on the wall." + end + end + + def verses(start, finish) + start.downto(finish).map { |n| verse(n) }.join("\n") << "\n" + end + + def numberBottles(n) + if n == 1 + "#{n} bottle" + else + "#{n} bottles" + end + end + + def sing + verses(99, 0) + end end diff --git a/bottles/spec/bottles_spec.rb b/bottles/spec/bottles_spec.rb index c974bfa..6466b31 100644 --- a/bottles/spec/bottles_spec.rb +++ b/bottles/spec/bottles_spec.rb @@ -10,37 +10,31 @@ end it "can sing another typical verse" do - skip expected = "3 bottles of beer on the wall, 3 bottles of beer.\nTake one down and pass it around, 2 bottles of beer on the wall.\n" expect( song.verse(3) ).to eq( expected ) end it "can sing about 1 bottle" do - skip expected = "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n" expect( song.verse(1) ).to eq( expected ) end it "can sing about 2 bottles" do - skip expected = "2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n" expect( song.verse(2) ).to eq( expected ) end it "can sing about no more bottles" do - skip expected = "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n" expect( song.verse(0) ).to eq( expected ) end it "can string a few verses together" do - skip expected = "8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it around, 7 bottles of beer on the wall.\n\n7 bottles of beer on the wall, 7 bottles of beer.\nTake one down and pass it around, 6 bottles of beer on the wall.\n\n6 bottles of beer on the wall, 6 bottles of beer.\nTake one down and pass it around, 5 bottles of beer on the wall.\n\n" expect( song.verses(8, 6) ).to eq( expected ) end it "can sing the whole song" do - skip expect( song.sing ).to eq( song.verses(99, 0) ) end end