Skip to content
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

Completed Homework #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Completed Homework #3

wants to merge 4 commits into from

Conversation

ralphos
Copy link

@ralphos ralphos commented Apr 28, 2012

I managed to complete the first assignment without any issues and writing the tests weren't a problem.

I got a little confused with the extra credit exercises.

For extra credit 1. I just created a Food class and converted the symbols to instances of Food. Not sure if this is what you were thinking?

For extra credit 2. I was also a little unsure as to what to do with a FoodBarge. I ended up creating a FoodBarge class, assuming that @foodbarge would be created as a new instance in IRB (since I found out that variables in your program aren't available in IRB). Now, when you call food_for on a FoodBarge object, it should check which class of animal it is dealing with and return an array of acceptable foods names' as strings.

So in IRB it'll be something like:

@foodbarge = FoodBarge.new
panda = Panda.new

food = @foodbarge.food_for(panda)
panda.feed(food)

When you call feed on panda it should go through the array of foods and if it likes? it it'll add to the meal count etc.

I got quite confused with writing the tests so I ended up writing the code and then trying to go back and write more tests. I guess my problem now is that I don't know what to test for exactly as well as being a little 'iffy' on rspec syntax.

@jwo
Copy link
Member

jwo commented Apr 29, 2012

Nicely done! What do you mean by "since I found out that variables in your program aren't available in IRB" ?

@ralphos
Copy link
Author

ralphos commented Apr 29, 2012

Thanks!

To answer your question, what I wanted to do was instantiate a FoodBarge object within zoo.rb and assign it an array of hashes containing each animals foods, so I could quickly reference it. For example,

@foodbarge = FoodBarge.new
@foodbarge = [ panda: [:bamboo, :apples], lion: [:wildebeests, :zebras], human: [:burgers, :fries] ]

But I soon found out that when I tried to call the variable in IRB it was nil.

@jwo
Copy link
Member

jwo commented Apr 29, 2012

Ahh, ok... When you go into IRB, if you say

require "./zoo"

Then it will execute everything in zoo... And if you have the @foodbarge = Foodbarge.new, then you'll be good to go.

In a real-life program, you'd probably have something like:

class Zoo
  attr_reader :food_barge

  def initialize
    @food_barge = Foodbarge.new
    # also implement #animals
  end

end

Then in irb, you'd:

@zoo = Zoo.new
zoo_keeper = ZooKeeper.new
@zoo.animals.each do |animal|
  zoo_keeper.feed(@zoo.food_barge.food_for(animal), to: animal)
end

@ralphos
Copy link
Author

ralphos commented Apr 29, 2012

Nice. That makes much more sense whereby all the animals, food_barge, zookeepers are all instances within the zoo class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants