From adb0274b65d26184a2946cdfa2240ad08e4c7b4d Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sat, 28 Apr 2012 18:06:28 -0500 Subject: [PATCH 1/4] Completed homework exercises --- zoo.rb | 53 +++++++++++++++++++++++++++++++++++++++++--- zoo_spec.rb | 63 +++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 99 insertions(+), 17 deletions(-) diff --git a/zoo.rb b/zoo.rb index 8f5eea5..9a684ce 100644 --- a/zoo.rb +++ b/zoo.rb @@ -13,7 +13,7 @@ def eat(food) end def likes?(food) - acceptable_food.include?(food.to_sym) + acceptable_food.include?(food) end def acceptable_food @@ -23,33 +23,70 @@ def acceptable_food def full? false end +end + +class Food + + attr_accessor :name + def initialize(name) + @name = name + end end +class FoodBarge + + def food_for(animal) + case animal + when Panda + animal.acceptable_food + when Lion + animal.acceptable_food + when Human + animal.acceptable_food + else + false + end + end +end class Panda include Animal def acceptable_food - [:bamboo] + bamboo = Food.new("bamboo") + [bamboo.name] end def full? @meals > 30 end + def feed(foods) + foods.each do |food| + eat(food) + end + end end class Lion include Animal def acceptable_food - [:wildebeests, :zeebras] + wildebeests = Food.new("wildebeests") + zebras = Food.new("zebras") + [wildebeests.name, zebras.name] end def full? @meals > 10 end + + def feed(foods) + foods.each do |food| + eat(food) + end + end end class Zookeeper @@ -58,6 +95,16 @@ def feed(args={}) panda = args.fetch(:to) panda.eat(food) end +end +class Human + include Animal + + def acceptable_food + [:bacon, :tacos] + bacon = Food.new("bacon") + tacos = Food.new("tacos") + [bacon.name, tacos.name] + end end diff --git a/zoo_spec.rb b/zoo_spec.rb index 4943c90..3b2d595 100644 --- a/zoo_spec.rb +++ b/zoo_spec.rb @@ -5,7 +5,7 @@ describe Panda do it "should like bamboo" do - Panda.new.likes?(:bamboo).should eq(true) + Panda.new.likes?("bamboo").should eq(true) end it "should like bamboo as a string" do @@ -13,50 +13,56 @@ end it "should not like grasshoppers" do - Panda.new.likes?(:grasshoppers).should eq(false) + Panda.new.likes?("grasshoppers").should eq(false) end it "should be able to eat the food" do - Panda.new.eat(:bamboo).should be_true + Panda.new.eat("bamboo").should be_true end it "should be full after eating 30 bamboo" do panda = Panda.new 31.times do - panda.eat(:bamboo) + panda.eat("bamboo") end panda.should be_full end it "should not be full after 1" do panda = Panda.new - panda.eat(:bamboo) + panda.eat("bamboo") panda.should_not be_full end + + it "should be able to get fed" do + panda = Panda.new + panda.should respond_to(:feed) + panda.feed(["bamboo"]) + end end describe Lion do it "should like wildebeests" do - Lion.new.likes?(:wildebeests).should eq(true) + Lion.new.likes?("wildebeests").should eq(true) end it "should like zeebras" do - Lion.new.likes?(:zeebras).should eq(true) + Lion.new.likes?("zebras").should eq(true) end it "should not like salad" do - Lion.new.likes?(:salad).should eq(false) + Lion.new.likes?("salad").should eq(false) end it "should take 11 meals to be full" do lion = Lion.new - lion.eat(:zeebras) + lion.eat("zeebras") lion.should_not be_full end it "should take 11 meals to be full" do lion = Lion.new 11.times do - lion.eat(:zeebras) + lion.eat("zebras") end lion.should be_full end @@ -65,13 +71,42 @@ describe Zookeeper do it "should be able to feed bamboo to the pandas" do panda = Panda.new - panda.should_receive(:eat).with(:bamboo) - Zookeeper.new.feed(food: :bamboo, to: panda) + panda.should_receive(:eat).with("bamboo") + Zookeeper.new.feed(food: "bamboo", to: panda) end it "should be able to feed zeebras to the lions" do lion = Lion.new - lion.should_receive(:eat).with(:zeebras) - Zookeeper.new.feed(food: :zeebras, to: lion) + lion.should_receive(:eat).with("zeebras") + Zookeeper.new.feed(food: "zeebras", to: lion) end end + +describe Human do + + it "should like bacon" do + Human.new.likes?("bacon").should be_true + end + + it "should like tacos" do + Human.new.likes?("tacos").should be_true + end + + it "should not like bamboo" do + Human.new.likes?("bamboo").should_not be_true + end + + it "should be able to eat" do + Human.new.eat("bacon").should be_true + end +end + +describe FoodBarge do + + it "should be able to check what foods are acceptable to the animal" do + foodbarge = FoodBarge.new + panda = Panda.new + foodbarge.should_receive(:food_for).with(panda) + foodbarge.food_for(panda) + end +end From d0d34ee359e5486f32b5cca19acf3fe9b2109150 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sat, 28 Apr 2012 18:26:42 -0500 Subject: [PATCH 2/4] Removed redundant line in Human class --- zoo.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/zoo.rb b/zoo.rb index 9a684ce..7b0f041 100644 --- a/zoo.rb +++ b/zoo.rb @@ -101,7 +101,6 @@ class Human include Animal def acceptable_food - [:bacon, :tacos] bacon = Food.new("bacon") tacos = Food.new("tacos") [bacon.name, tacos.name] From 60385d4a062e82d5e052d741d69000a8c6225e78 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sat, 28 Apr 2012 18:34:22 -0500 Subject: [PATCH 3/4] Removed more redundant code and moved feed method into Animal module --- zoo.rb | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/zoo.rb b/zoo.rb index 7b0f041..cc2b54b 100644 --- a/zoo.rb +++ b/zoo.rb @@ -23,6 +23,12 @@ def acceptable_food def full? false end + + def feed(foods) + foods.each do |food| + eat(food) + end + end end class Food @@ -37,16 +43,7 @@ def initialize(name) class FoodBarge def food_for(animal) - case animal - when Panda - animal.acceptable_food - when Lion - animal.acceptable_food - when Human - animal.acceptable_food - else - false - end + animal.acceptable_food end end @@ -62,11 +59,6 @@ def full? @meals > 30 end - def feed(foods) - foods.each do |food| - eat(food) - end - end end class Lion @@ -81,12 +73,6 @@ def acceptable_food def full? @meals > 10 end - - def feed(foods) - foods.each do |food| - eat(food) - end - end end class Zookeeper From 9596e86b51527e29eafe51c64c64dbb831bed860 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sun, 29 Apr 2012 14:56:24 -0500 Subject: [PATCH 4/4] Refactored solution by referencing food objects in rspec tests --- zoo.rb | 15 ++++++--------- zoo_spec.rb | 40 +++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/zoo.rb b/zoo.rb index cc2b54b..381009f 100644 --- a/zoo.rb +++ b/zoo.rb @@ -13,7 +13,7 @@ def eat(food) end def likes?(food) - acceptable_food.include?(food) + acceptable_food.include?(food.name) end def acceptable_food @@ -42,6 +42,8 @@ def initialize(name) class FoodBarge + @foodbarge = FoodBarge.new + def food_for(animal) animal.acceptable_food end @@ -51,8 +53,7 @@ class Panda include Animal def acceptable_food - bamboo = Food.new("bamboo") - [bamboo.name] + [Food.new("bamboo").name] end def full? @@ -65,9 +66,7 @@ class Lion include Animal def acceptable_food - wildebeests = Food.new("wildebeests") - zebras = Food.new("zebras") - [wildebeests.name, zebras.name] + [Food.new("wildebeests").name, Food.new("zebras").name] end def full? @@ -87,9 +86,7 @@ class Human include Animal def acceptable_food - bacon = Food.new("bacon") - tacos = Food.new("tacos") - [bacon.name, tacos.name] + [Food.new("bacon").name, Food.new("tacos").name] end end diff --git a/zoo_spec.rb b/zoo_spec.rb index 3b2d595..9fff70e 100644 --- a/zoo_spec.rb +++ b/zoo_spec.rb @@ -5,64 +5,60 @@ describe Panda do it "should like bamboo" do - Panda.new.likes?("bamboo").should eq(true) - end - - it "should like bamboo as a string" do - Panda.new.likes?("bamboo").should eq(true) + Panda.new.likes?(Food.new("bamboo")).should eq(true) end it "should not like grasshoppers" do - Panda.new.likes?("grasshoppers").should eq(false) + Panda.new.likes?(Food.new("grasshoppers")).should eq(false) end it "should be able to eat the food" do - Panda.new.eat("bamboo").should be_true + Panda.new.eat(Food.new("bamboo")).should be_true end it "should be full after eating 30 bamboo" do panda = Panda.new 31.times do - panda.eat("bamboo") + panda.eat(Food.new("bamboo")) end panda.should be_full end it "should not be full after 1" do panda = Panda.new - panda.eat("bamboo") + panda.eat(Food.new("bamboo")) panda.should_not be_full end it "should be able to get fed" do panda = Panda.new panda.should respond_to(:feed) - panda.feed(["bamboo"]) + panda.feed([Food.new("bamboo")]) end end describe Lion do it "should like wildebeests" do - Lion.new.likes?("wildebeests").should eq(true) + Lion.new.likes?(Food.new("wildebeests")).should eq(true) end it "should like zeebras" do - Lion.new.likes?("zebras").should eq(true) + Lion.new.likes?(Food.new("zebras")).should eq(true) end it "should not like salad" do - Lion.new.likes?("salad").should eq(false) + Lion.new.likes?(Food.new("salad")).should eq(false) end it "should take 11 meals to be full" do lion = Lion.new - lion.eat("zeebras") + lion.eat(Food.new("zeebras")) lion.should_not be_full end it "should take 11 meals to be full" do lion = Lion.new 11.times do - lion.eat("zebras") + lion.eat(Food.new("zebras")) end lion.should be_full end @@ -85,19 +81,19 @@ describe Human do it "should like bacon" do - Human.new.likes?("bacon").should be_true + Human.new.likes?(Food.new("bacon")).should be_true end it "should like tacos" do - Human.new.likes?("tacos").should be_true + Human.new.likes?(Food.new("tacos")).should be_true end it "should not like bamboo" do - Human.new.likes?("bamboo").should_not be_true + Human.new.likes?(Food.new("bamboo")).should_not be_true end it "should be able to eat" do - Human.new.eat("bacon").should be_true + Human.new.eat(Food.new("bacon")).should be_true end end @@ -109,4 +105,10 @@ foodbarge.should_receive(:food_for).with(panda) foodbarge.food_for(panda) end + + it "should have bacon and tacos ready for the human to eat" do + animal = Human.new + barge = FoodBarge.new + barge.food_for(animal).should eq([Food.new("bacon").name, Food.new("tacos").name]) + end end