Skip to content

Panda and Tiger Levels #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.rspec
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
35 changes: 32 additions & 3 deletions zoo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Animal

def eat(food)
@meals ||= 0
if likes?(food)
@meals += 1
true
Expand All @@ -26,10 +25,22 @@ def full?

end

class Human
include Animal

def acceptable_food
[Bacon.new, Tacos.new]
end
end


class Panda
include Animal

def initialize
@meals ||= 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the initialize method, you can safely assume that @meals has not been set. so I would do

def initialize
  @meals = 0
end

end

def acceptable_food
[Bamboo.new]
end
Expand All @@ -38,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
Expand All @@ -65,13 +84,23 @@ 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={})
food = args.fetch(:food)
panda = args.fetch(:to)
panda.eat(food)
animal = args.fetch(:to)
if animal.full?
puts "The #{animal} is full."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the Zookeeper should "puts" anything. What do you think is an alternative here?

If the animal eats, we get returned a true. Should we get a false if not?

else
animal.eat(food)
end
end
end

class Foodbarge
def food_for(animal)
animal.acceptable_food
end
end

39 changes: 37 additions & 2 deletions zoo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,23 @@ 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

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
Expand Down Expand Up @@ -67,6 +74,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
Expand All @@ -79,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