From 2b866f226ef8ef40d71c91b351aa47d2d80784c6 Mon Sep 17 00:00:00 2001 From: jason perez Date: Thu, 2 Jan 2014 14:13:30 -0800 Subject: [PATCH 1/2] create a human that likes bacon and tacos, but not bamboo --- .gitignore | 1 + README.md | 2 +- zoo.rb | 9 +++++++++ zoo_spec.rb | 16 ++++++++++++++-- 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4cde7c9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.rspec \ No newline at end of file diff --git a/README.md b/README.md index 9120405..8dda68d 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Panda Level Tiger Level --------------- 1. Create a FoodBarge that can be called like: -2. Test that when the zookeepers gets food for the panda, +2. Test that when the zookeepers gets food for the panda, the panda will eat it ``` diff --git a/zoo.rb b/zoo.rb index d4d906c..8aead5a 100644 --- a/zoo.rb +++ b/zoo.rb @@ -26,6 +26,14 @@ def full? end +class Human + include Animal + + def acceptable_food + [Bacon.new, Tacos.new] + end +end + class Panda include Animal @@ -65,6 +73,7 @@ class Tacos < Food; end class Wildebeests < Food; end class Zeebras < Food; end class Bamboo < Food; end +class Bacon < Food; end class Zookeeper def feed(args={}) diff --git a/zoo_spec.rb b/zoo_spec.rb index 64203cc..da7f4cf 100644 --- a/zoo_spec.rb +++ b/zoo_spec.rb @@ -28,14 +28,14 @@ class Salad < Food; end it "should be full after eating 30 bamboo" do panda = Panda.new 31.times do - panda.eat(Bamboo.new) + panda.eat(Bamboo.new) end panda.should be_full end it "should not be full after 1" do panda = Panda.new - panda.eat(Bamboo.new) + panda.eat(Bamboo.new) panda.should_not be_full end end @@ -67,6 +67,18 @@ class Salad < Food; end end end +describe Human do + it "should like bacon" do + Human.new.likes?(Bacon.new).should eq(true) + end + it "should like tacos" do + Human.new.likes?(Tacos.new).should eq(true) + end + it "should not like bamboo" do + Human.new.likes?(Bamboo.new).should eq(false) + end +end + describe Zookeeper do it "should be able to feed bamboo to the pandas" do panda = Panda.new From 5a640de4cf636deb77131aca97b1f8363c1a58ac Mon Sep 17 00:00:00 2001 From: jason perez Date: Fri, 3 Jan 2014 08:58:55 -0800 Subject: [PATCH 2/2] add a human, add a foodbarge, add ability for zookeeper to stop feeding panda when panda is full --- zoo.rb | 26 +++++++++++++++++++++++--- zoo_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/zoo.rb b/zoo.rb index 8aead5a..d111823 100644 --- a/zoo.rb +++ b/zoo.rb @@ -3,7 +3,6 @@ module Animal def eat(food) - @meals ||= 0 if likes?(food) @meals += 1 true @@ -38,6 +37,10 @@ def acceptable_food class Panda include Animal + def initialize + @meals ||= 0 + end + def acceptable_food [Bamboo.new] end @@ -46,11 +49,19 @@ def full? @meals > 30 end + def feed(food) + acceptable_food == food + end + end class Lion include Animal + def initialize + @meals ||= 0 + end + def acceptable_food [Wildebeests.new, Zeebras.new] end @@ -78,9 +89,18 @@ class Bacon < Food; end class Zookeeper def feed(args={}) food = args.fetch(:food) - panda = args.fetch(:to) - panda.eat(food) + animal = args.fetch(:to) + if animal.full? + puts "The #{animal} is full." + else + animal.eat(food) + end end +end +class Foodbarge + def food_for(animal) + animal.acceptable_food + end end diff --git a/zoo_spec.rb b/zoo_spec.rb index da7f4cf..794f2cc 100644 --- a/zoo_spec.rb +++ b/zoo_spec.rb @@ -38,6 +38,13 @@ class Salad < Food; end panda.eat(Bamboo.new) panda.should_not be_full end + + it "should be able to eat panda food from the foodbarge" do + foodbarge = Foodbarge.new + panda = Panda.new + food = foodbarge.food_for(panda) + panda.feed(food).should eq(true) + end end describe Lion do @@ -91,4 +98,20 @@ class Salad < Food; end lion.should_receive(:eat).with(:zeebras) Zookeeper.new.feed(food: :zeebras, to: lion) end + + it "should stop feeding panda when panda is full" do + panda = Panda.new + keeper = Zookeeper.new + 31.times { keeper.feed(food: Bamboo.new, to: panda) } + panda.should_not_receive(:eat) + keeper.feed(food: Bamboo.new, to: panda) + end +end + +describe Foodbarge do + it "should be able to get food for panda" do + foodbarge = Foodbarge.new + panda = Panda.new + foodbarge.food_for(panda).should eq([Bamboo.new]) + end end