From 2cc07603bb0995cb39c67efbcdc1f27255a9e61a Mon Sep 17 00:00:00 2001 From: Dan Brooks Date: Mon, 25 Sep 2017 10:12:26 +0100 Subject: [PATCH 01/11] Fix first two failing specs --- bottles/lib/bottles.rb | 7 +++++++ bottles/spec/bottles_spec.rb | 2 -- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/bottles/lib/bottles.rb b/bottles/lib/bottles.rb index 05185b1..e63a65c 100644 --- a/bottles/lib/bottles.rb +++ b/bottles/lib/bottles.rb @@ -1,2 +1,9 @@ class Bottles + def verse(n) + if n == 8 + "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" + elsif n == 3 + "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" + end + end end diff --git a/bottles/spec/bottles_spec.rb b/bottles/spec/bottles_spec.rb index c974bfa..faf129b 100644 --- a/bottles/spec/bottles_spec.rb +++ b/bottles/spec/bottles_spec.rb @@ -10,13 +10,11 @@ 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 From 2570d12c25fe2e52d3b564af241ea3cddb6d0a5c Mon Sep 17 00:00:00 2001 From: Dan Brooks Date: Mon, 25 Sep 2017 10:13:56 +0100 Subject: [PATCH 02/11] 3rd test --- bottles/lib/bottles.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bottles/lib/bottles.rb b/bottles/lib/bottles.rb index e63a65c..dd5947e 100644 --- a/bottles/lib/bottles.rb +++ b/bottles/lib/bottles.rb @@ -4,6 +4,8 @@ def verse(n) "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" elsif n == 3 "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" + elsif n == 1 + "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" end end end From 08b493e20c87539998c7b5c1ceab4b6779503297 Mon Sep 17 00:00:00 2001 From: Dan Brooks Date: Mon, 25 Sep 2017 10:19:16 +0100 Subject: [PATCH 03/11] Fix fourth test --- bottles/lib/bottles.rb | 2 ++ bottles/spec/bottles_spec.rb | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/bottles/lib/bottles.rb b/bottles/lib/bottles.rb index dd5947e..e1934ba 100644 --- a/bottles/lib/bottles.rb +++ b/bottles/lib/bottles.rb @@ -6,6 +6,8 @@ def verse(n) "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" elsif n == 1 "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" + elsif n == 2 + "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" end end end diff --git a/bottles/spec/bottles_spec.rb b/bottles/spec/bottles_spec.rb index faf129b..de634f8 100644 --- a/bottles/spec/bottles_spec.rb +++ b/bottles/spec/bottles_spec.rb @@ -20,7 +20,6 @@ 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 From f0c39177e3a2fe279892c35842b77daa4cbc8571 Mon Sep 17 00:00:00 2001 From: Dan Brooks Date: Mon, 25 Sep 2017 10:20:16 +0100 Subject: [PATCH 04/11] Fix fifth spec --- bottles/lib/bottles.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bottles/lib/bottles.rb b/bottles/lib/bottles.rb index e1934ba..181b594 100644 --- a/bottles/lib/bottles.rb +++ b/bottles/lib/bottles.rb @@ -8,6 +8,8 @@ def verse(n) "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" elsif n == 2 "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" + elsif n == 0 + "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" end end end From 6d05359fd8ae0a84462bdffef74ecfa09e6d3fde Mon Sep 17 00:00:00 2001 From: Dan Brooks Date: Mon, 25 Sep 2017 10:36:26 +0100 Subject: [PATCH 05/11] Get tests 5 and 6 to pass --- bottles/lib/bottles.rb | 8 ++++++++ bottles/spec/bottles_spec.rb | 2 -- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/bottles/lib/bottles.rb b/bottles/lib/bottles.rb index 181b594..3842bb4 100644 --- a/bottles/lib/bottles.rb +++ b/bottles/lib/bottles.rb @@ -12,4 +12,12 @@ def verse(n) "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" end end + + def verses(start, finish) + lines = [99] + + if start == 8 && finish == 6 + "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" + end + end end diff --git a/bottles/spec/bottles_spec.rb b/bottles/spec/bottles_spec.rb index de634f8..7fe11ca 100644 --- a/bottles/spec/bottles_spec.rb +++ b/bottles/spec/bottles_spec.rb @@ -25,13 +25,11 @@ 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 From 5b2b0c6cde8714257ef09d542c27b4a0f8d80879 Mon Sep 17 00:00:00 2001 From: Dan Brooks Date: Mon, 25 Sep 2017 11:01:05 +0100 Subject: [PATCH 06/11] refactoring to handle plurality in bottle(s) --- bottles/lib/bottles.rb | 33 +++++++++++++++++++++++---------- bottles/spec/bottles_spec.rb | 1 + 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/bottles/lib/bottles.rb b/bottles/lib/bottles.rb index 3842bb4..540d06b 100644 --- a/bottles/lib/bottles.rb +++ b/bottles/lib/bottles.rb @@ -1,15 +1,20 @@ class Bottles + def verse(n) - if n == 8 - "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" - elsif n == 3 - "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" - elsif n == 1 - "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" - elsif n == 2 - "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" - elsif n == 0 - "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" + if n > 0 + "#{prefix(n)}\n#{suffix(n)}\n" + end + end + + def prefix(n) + "#{numberBottles(n)} of beer on the wall, #{numberBottles(n)} of beer." + end + + def suffix(n) + if 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 @@ -20,4 +25,12 @@ def verses(start, finish) "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" end end + + def numberBottles(n) + if n == 1 + "#{n} bottle" + else + "#{n} bottles" + end + end end diff --git a/bottles/spec/bottles_spec.rb b/bottles/spec/bottles_spec.rb index 7fe11ca..89c2be5 100644 --- a/bottles/spec/bottles_spec.rb +++ b/bottles/spec/bottles_spec.rb @@ -25,6 +25,7 @@ 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 From 1b22df3a34363d401245f201c9daeac71a4b84a4 Mon Sep 17 00:00:00 2001 From: Dan Brooks Date: Mon, 25 Sep 2017 11:04:22 +0100 Subject: [PATCH 07/11] another spec passing --- bottles/lib/bottles.rb | 4 +++- bottles/spec/bottles_spec.rb | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/bottles/lib/bottles.rb b/bottles/lib/bottles.rb index 540d06b..e1bf4f8 100644 --- a/bottles/lib/bottles.rb +++ b/bottles/lib/bottles.rb @@ -1,7 +1,9 @@ class Bottles def verse(n) - if n > 0 + if n == 0 + "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" + else "#{prefix(n)}\n#{suffix(n)}\n" end end diff --git a/bottles/spec/bottles_spec.rb b/bottles/spec/bottles_spec.rb index 89c2be5..7fe11ca 100644 --- a/bottles/spec/bottles_spec.rb +++ b/bottles/spec/bottles_spec.rb @@ -25,7 +25,6 @@ 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 From fb140f9738ea6ffbd101336d396c15684c018fe0 Mon Sep 17 00:00:00 2001 From: Dan Brooks Date: Mon, 25 Sep 2017 11:41:53 +0100 Subject: [PATCH 08/11] Get all tests passing; --- bottles/lib/bottles.rb | 14 ++++++++++---- bottles/spec/bottles_spec.rb | 1 - 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/bottles/lib/bottles.rb b/bottles/lib/bottles.rb index e1bf4f8..644de3b 100644 --- a/bottles/lib/bottles.rb +++ b/bottles/lib/bottles.rb @@ -21,11 +21,13 @@ def suffix(n) end def verses(start, finish) - lines = [99] - - if start == 8 && finish == 6 - "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" + number_of_verses = start + song = "" + while number_of_verses > (finish - 1) do + song << verse(number_of_verses) << "\n" + number_of_verses -= 1 end + return song end def numberBottles(n) @@ -35,4 +37,8 @@ def numberBottles(n) "#{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 7fe11ca..6466b31 100644 --- a/bottles/spec/bottles_spec.rb +++ b/bottles/spec/bottles_spec.rb @@ -35,7 +35,6 @@ end it "can sing the whole song" do - skip expect( song.sing ).to eq( song.verses(99, 0) ) end end From c1bf636be21357de3aeb08009e77e8af6bcc1815 Mon Sep 17 00:00:00 2001 From: Dan Brooks Date: Mon, 25 Sep 2017 11:43:07 +0100 Subject: [PATCH 09/11] Refactor using downto --- bottles/lib/bottles.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/bottles/lib/bottles.rb b/bottles/lib/bottles.rb index 644de3b..99ba207 100644 --- a/bottles/lib/bottles.rb +++ b/bottles/lib/bottles.rb @@ -21,13 +21,11 @@ def suffix(n) end def verses(start, finish) - number_of_verses = start song = "" - while number_of_verses > (finish - 1) do - song << verse(number_of_verses) << "\n" - number_of_verses -= 1 + start.downto(finish) do |n| + song << verse(n) << "\n" end - return song + song end def numberBottles(n) From 609b59c8c1014fe28d19b5fe9a62c11e2a9a6a5c Mon Sep 17 00:00:00 2001 From: Dan Brooks Date: Mon, 25 Sep 2017 11:46:33 +0100 Subject: [PATCH 10/11] Refactor using map/join --- bottles/lib/bottles.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/bottles/lib/bottles.rb b/bottles/lib/bottles.rb index 99ba207..9ed8931 100644 --- a/bottles/lib/bottles.rb +++ b/bottles/lib/bottles.rb @@ -1,5 +1,4 @@ class Bottles - def verse(n) if n == 0 "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" @@ -21,11 +20,7 @@ def suffix(n) end def verses(start, finish) - song = "" - start.downto(finish) do |n| - song << verse(n) << "\n" - end - song + start.downto(finish).map { |n| verse(n) }.join("\n") << "\n" end def numberBottles(n) From 7d1c737c30e1abea6dc8e564108b884c834cdd7e Mon Sep 17 00:00:00 2001 From: Dan Brooks Date: Mon, 25 Sep 2017 11:53:24 +0100 Subject: [PATCH 11/11] Final refactoring --- bottles/lib/bottles.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/bottles/lib/bottles.rb b/bottles/lib/bottles.rb index 9ed8931..a5f3aa9 100644 --- a/bottles/lib/bottles.rb +++ b/bottles/lib/bottles.rb @@ -1,18 +1,20 @@ class Bottles def verse(n) - if n == 0 - "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" - else - "#{prefix(n)}\n#{suffix(n)}\n" - end + "#{prefix(n)}\n#{suffix(n)}\n" end def prefix(n) - "#{numberBottles(n)} of beer on the wall, #{numberBottles(n)} of beer." + 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 == 1 + 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."