From a6bdf1db2b99230782dcfe4221d5b4eb0ae0a98f Mon Sep 17 00:00:00 2001 From: Richard Tillies Date: Thu, 31 Mar 2022 09:34:34 -0400 Subject: [PATCH 1/9] feat: Candy init --- objects-and-methods/exercise-1/lib/candy.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 objects-and-methods/exercise-1/lib/candy.rb diff --git a/objects-and-methods/exercise-1/lib/candy.rb b/objects-and-methods/exercise-1/lib/candy.rb new file mode 100644 index 00000000..2c079b88 --- /dev/null +++ b/objects-and-methods/exercise-1/lib/candy.rb @@ -0,0 +1,7 @@ +class Candy + attr_reader :type + + def initialize(type) + @type = type + end +end From 44aab3ec1f32e9b9518a844676aaca86de2bb7ca Mon Sep 17 00:00:00 2001 From: Richard Tillies Date: Thu, 31 Mar 2022 09:46:45 -0400 Subject: [PATCH 2/9] Manual reset --- objects-and-methods/exercise-1/lib/candy.rb | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 objects-and-methods/exercise-1/lib/candy.rb diff --git a/objects-and-methods/exercise-1/lib/candy.rb b/objects-and-methods/exercise-1/lib/candy.rb deleted file mode 100644 index 2c079b88..00000000 --- a/objects-and-methods/exercise-1/lib/candy.rb +++ /dev/null @@ -1,7 +0,0 @@ -class Candy - attr_reader :type - - def initialize(type) - @type = type - end -end From 6f0969dffb04e2982100c968754b7030543f0965 Mon Sep 17 00:00:00 2001 From: Richard Tillies Date: Thu, 31 Mar 2022 15:40:18 -0400 Subject: [PATCH 3/9] Fix syntax error in expect statement --- objects-and-methods/exercise-1/spec/bag_spec.rb | 2 +- objects-and-methods/exercise-2/spec/bag_spec.rb | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/objects-and-methods/exercise-1/spec/bag_spec.rb b/objects-and-methods/exercise-1/spec/bag_spec.rb index 4e20cd51..00ca4d13 100644 --- a/objects-and-methods/exercise-1/spec/bag_spec.rb +++ b/objects-and-methods/exercise-1/spec/bag_spec.rb @@ -56,6 +56,6 @@ bag << Candy.new("Lindt chocolate") expect(bag.contains?('Lindt chocolate')).to be true - expect(bag.contains?('Nerds')).to false + expect(bag.contains?('Nerds')).to be false end end diff --git a/objects-and-methods/exercise-2/spec/bag_spec.rb b/objects-and-methods/exercise-2/spec/bag_spec.rb index 3733387e..92f65aa3 100644 --- a/objects-and-methods/exercise-2/spec/bag_spec.rb +++ b/objects-and-methods/exercise-2/spec/bag_spec.rb @@ -56,7 +56,7 @@ bag << Candy.new('Lindt chocolate') expect(bag.contains?('Lindt chocolate')).to be true - expect(bag.contains?('Nerds')).to false + expect(bag.contains?('Nerds')).to be false end xit 'can get a particular type of candy' do @@ -104,7 +104,3 @@ expect(candy.type).to eq('Lifesavers') end end - - - - From 62f75d34fd84ff4bc11bf16cf1be5e9fcad7a1dc Mon Sep 17 00:00:00 2001 From: Richard Tillies Date: Thu, 31 Mar 2022 16:34:29 -0400 Subject: [PATCH 4/9] test: separate Bag.new from expect statement in first three tests --- objects-and-methods/exercise-1/spec/bag_spec.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/objects-and-methods/exercise-1/spec/bag_spec.rb b/objects-and-methods/exercise-1/spec/bag_spec.rb index 00ca4d13..56250359 100644 --- a/objects-and-methods/exercise-1/spec/bag_spec.rb +++ b/objects-and-methods/exercise-1/spec/bag_spec.rb @@ -4,22 +4,26 @@ RSpec.describe Bag do it 'is empty' do - expect(Bag.new.empty?).to be true + bag = Bag.new + + expect(bag.empty?).to be true end xit 'can count the candy in an empty bag' do + bag = Bag.new + expect(Bag.new.count).to eq(0) end xit 'has no candies when it is empty' do + bag = Bag.new + expect(Bag.new.candies).to eq([]) end xit 'can put a candy in a bag' do bag = Bag.new - candy = Candy.new('Sour frogs') - bag << candy expect(bag.candies).to eq([candy]) From 6a44d639bc756d42205e65ae6fe941114e0d5317 Mon Sep 17 00:00:00 2001 From: Richard Tillies Date: Thu, 31 Mar 2022 16:36:05 -0400 Subject: [PATCH 5/9] test: Refactor << to #add_candy method --- objects-and-methods/exercise-1/spec/bag_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/objects-and-methods/exercise-1/spec/bag_spec.rb b/objects-and-methods/exercise-1/spec/bag_spec.rb index 56250359..94458b80 100644 --- a/objects-and-methods/exercise-1/spec/bag_spec.rb +++ b/objects-and-methods/exercise-1/spec/bag_spec.rb @@ -5,7 +5,7 @@ RSpec.describe Bag do it 'is empty' do bag = Bag.new - + expect(bag.empty?).to be true end @@ -24,28 +24,28 @@ xit 'can put a candy in a bag' do bag = Bag.new candy = Candy.new('Sour frogs') - bag << candy + bag.add_candy candy expect(bag.candies).to eq([candy]) end xit 'is not empty when it has candies' do bag = Bag.new - bag << Candy.new("Nerds") + bag.add_candy Candy.new("Nerds") expect(bag.empty?).to be false end xit 'can count candies' do bag = Bag.new - bag << Candy.new("Caramelized Almonds") + bag.add_candy Candy.new("Caramelized Almonds") expect(bag.count).to eq(1) end xit 'contains candies and candies have a type' do bag = Bag.new - bag << Candy.new("Hershey's Kisses") + bag.add_candy Candy.new("Hershey's Kisses") # You usually don't want to chain a bunch of different # types of things together like this. # We'll talk about it more in a few weeks. @@ -57,7 +57,7 @@ xit 'can be asked if it has a particular kind of candy' do bag = Bag.new - bag << Candy.new("Lindt chocolate") + bag.add_candy Candy.new("Lindt chocolate") expect(bag.contains?('Lindt chocolate')).to be true expect(bag.contains?('Nerds')).to be false From 0a495a835685ec888454f519aa6ea1f4958266ab Mon Sep 17 00:00:00 2001 From: Richard Tillies Date: Thu, 31 Mar 2022 16:42:28 -0400 Subject: [PATCH 6/9] test: revise note about chaining methods --- objects-and-methods/exercise-1/spec/bag_spec.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/objects-and-methods/exercise-1/spec/bag_spec.rb b/objects-and-methods/exercise-1/spec/bag_spec.rb index 94458b80..b8201f46 100644 --- a/objects-and-methods/exercise-1/spec/bag_spec.rb +++ b/objects-and-methods/exercise-1/spec/bag_spec.rb @@ -43,13 +43,15 @@ expect(bag.count).to eq(1) end + # NOTE: + # You usually don't want to chain a bunch of + # different methods together like this: + # type = bag.candies.first.type + # We'll talk about it more in a few weeks. + # However. it's important to understand how these methods work. xit 'contains candies and candies have a type' do bag = Bag.new bag.add_candy Candy.new("Hershey's Kisses") - # You usually don't want to chain a bunch of different - # types of things together like this. - # We'll talk about it more in a few weeks. - # It's important to understand how these methods work, though. type = bag.candies.first.type expect(type).to eq("Hershey's Kisses") From 6142f6c84338207724f010e7824ef28c9f1ec2a8 Mon Sep 17 00:00:00 2001 From: Richard Tillies Date: Thu, 31 Mar 2022 16:45:37 -0400 Subject: [PATCH 7/9] test: copy changes from exercise-1/spec/bag_spec.rb --- .../exercise-2/spec/bag_spec.rb | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/objects-and-methods/exercise-2/spec/bag_spec.rb b/objects-and-methods/exercise-2/spec/bag_spec.rb index 92f65aa3..535b297e 100644 --- a/objects-and-methods/exercise-2/spec/bag_spec.rb +++ b/objects-and-methods/exercise-2/spec/bag_spec.rb @@ -4,56 +4,62 @@ RSpec.describe Bag do it 'is empty' do - expect(Bag.new.empty?).to be true + bag = Bag.new + + expect(bag.empty?).to be true end xit 'can count the candy in an empty bag' do + bag = Bag.new + expect(Bag.new.count).to eq(0) end xit 'has no candies when it is empty' do + bag = Bag.new + expect(Bag.new.candies).to eq([]) end xit 'can put a candy in a bag' do bag = Bag.new - candy = Candy.new('Sour frogs') - - bag << candy + bag.add_candy candy expect(bag.candies).to eq([candy]) end xit 'is not empty when it has candies' do bag = Bag.new - bag << Candy.new('Nerds') + bag.add_candy Candy.new("Nerds") expect(bag.empty?).to be false end xit 'can count candies' do bag = Bag.new - bag << Candy.new('Caramelized Almonds') + bag.add_candy Candy.new("Caramelized Almonds") expect(bag.count).to eq(1) end + # NOTE: + # You usually don't want to chain a bunch of + # different methods together like this: + # type = bag.candies.first.type + # We'll talk about it more in a few weeks. + # However. it's important to understand how these methods work. xit 'contains candies and candies have a type' do bag = Bag.new - bag << Candy.new('Hersheys Kisses') - # You usually don't want to chain a bunch of different - # types of things together like this. - # We'll talk about it more in a few weeks. - # It's important to understand how these methods work, though. + bag.add_candy Candy.new("Hershey's Kisses") type = bag.candies.first.type - expect(type).to eq('Hersheys Kisses') + expect(type).to eq("Hershey's Kisses") end xit 'can be asked if it has a particular kind of candy' do bag = Bag.new - bag << Candy.new('Lindt chocolate') + bag.add_candy Candy.new("Lindt chocolate") expect(bag.contains?('Lindt chocolate')).to be true expect(bag.contains?('Nerds')).to be false From 1411f94db6284863d42b9687960168c5498c8537 Mon Sep 17 00:00:00 2001 From: Richard Tillies Date: Thu, 31 Mar 2022 16:48:38 -0400 Subject: [PATCH 8/9] test: Refactor << to #add_candy method --- .../exercise-1/spec/trick_or_treater_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/objects-and-methods/exercise-1/spec/trick_or_treater_spec.rb b/objects-and-methods/exercise-1/spec/trick_or_treater_spec.rb index 31b319a8..cf243141 100644 --- a/objects-and-methods/exercise-1/spec/trick_or_treater_spec.rb +++ b/objects-and-methods/exercise-1/spec/trick_or_treater_spec.rb @@ -31,7 +31,7 @@ xit 'can get candies' do trick_or_treater = TrickOrTreater.new(Costume.new('Spaceship Mechanic')) - trick_or_treater.bag << Candy.new('Gummy bears') + trick_or_treater.bag.add_candy Candy.new('Gummy bears') expect(trick_or_treater.has_candy?).to be true end @@ -41,16 +41,16 @@ expect(trick_or_treater.candy_count).to eq(0) - trick_or_treater.bag << Candy.new('Gummy bears') + trick_or_treater.bag.add_candy Candy.new('Gummy bears') expect(trick_or_treater.candy_count).to eq(1) end xit 'can eat candies' do trick_or_treater = TrickOrTreater.new(Costume.new("Baron")) - trick_or_treater.bag << Candy.new("Gummy worms") - trick_or_treater.bag << Candy.new("Liquorice") - trick_or_treater.bag << Candy.new("Salty Serpents") + trick_or_treater.bag.add_candy Candy.new("Gummy worms") + trick_or_treater.bag.add_candy Candy.new("Liquorice") + trick_or_treater.bag.add_candy Candy.new("Salty Serpents") expect(trick_or_treater.candy_count).to eq(3) trick_or_treater.eat From f88577d0eee5cc30b30c13b7576b0bd027c6c589 Mon Sep 17 00:00:00 2001 From: Richard Tillies Date: Thu, 31 Mar 2022 16:49:47 -0400 Subject: [PATCH 9/9] test: Refactor << to #add_candy method --- .../exercise-2/spec/trick_or_treater_spec.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/objects-and-methods/exercise-2/spec/trick_or_treater_spec.rb b/objects-and-methods/exercise-2/spec/trick_or_treater_spec.rb index 2fa35907..123a514a 100644 --- a/objects-and-methods/exercise-2/spec/trick_or_treater_spec.rb +++ b/objects-and-methods/exercise-2/spec/trick_or_treater_spec.rb @@ -31,7 +31,7 @@ xit 'can get candies' do trick_or_treater = TrickOrTreater.new(Costume.new('Spaceship Mechanic')) - trick_or_treater.bag << Candy.new('Gummy bears') + trick_or_treater.bag.add_candy Candy.new('Gummy bears') expect(trick_or_treater.has_candy?).to be true end @@ -41,16 +41,16 @@ expect(trick_or_treater.candy_count).to eq(0) - trick_or_treater.bag << Candy.new('Gummy bears') + trick_or_treater.bag.add_candy Candy.new('Gummy bears') expect(trick_or_treater.candy_count).to eq(1) end xit 'can eat candies' do trick_or_treater = TrickOrTreater.new(Costume.new('Baron')) - trick_or_treater.bag << Candy.new('Gummy worms') - trick_or_treater.bag << Candy.new('Liquorice') - trick_or_treater.bag << Candy.new('Salty Serpents') + trick_or_treater.bag.add_candy Candy.new('Gummy worms') + trick_or_treater.bag.add_candy Candy.new('Liquorice') + trick_or_treater.bag.add_candy Candy.new('Salty Serpents') expect(trick_or_treater.candy_count).to eq(3) trick_or_treater.eat @@ -73,9 +73,9 @@ xit 'increases the sugar level when it eats candies' do trick_or_treater = TrickOrTreater.new(Costume.new('Hobbit')) - trick_or_treater.bag << Candy.new('Gummy worms', 88) - trick_or_treater.bag << Candy.new('Liquorice', 83) - trick_or_treater.bag << Candy.new('Salty Serpents', 71) + trick_or_treater.bag.add_candy Candy.new('Gummy worms', 88) + trick_or_treater.bag.add_candy Candy.new('Liquorice', 83) + trick_or_treater.bag.add_candy Candy.new('Salty Serpents', 71) trick_or_treater.eat trick_or_treater.eat