I'm working on a problem in Chris Pine's Learn to Program book. I've defined a class Pet, with some instance methods. Outside of the class definition, I'm trying to build a method that will take a string and an instance of the Pet class and run the appropriate instance method.
def dispatch(command, pet)
dispatches = {'feed' => pet.feed,
'walk' => pet.walk,
'put to bed' => pet.putToBed,
'rock' => pet.rock,
'toss' => pet.toss}
dispatches[command]
end
When the dispatch method runs, however, it executes all the instance methods that appear in the hash, not just the one corresponding to command. They execute in the order they appear in the code, and before even reaching the dispatches[command] line.
What am I doing wrong here?