1

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?

1
  • give the class body also.. so that we can run Sep 21, 2013 at 18:21

2 Answers 2

3

Yes, that is correct behaviour.

{'feed'       => pet.feed}

The line above means "call method pet.feed and use its return value for assigning to key 'feed'". You have to use lambdas (or similar) to create chunks of code that can be invoked later. Something like this:

def dispatch(command, pet)
  dispatches = {'feed'       => proc { pet.feed },
                'put to bed' => proc { pet.put_to_bed }}
  dispatches[command].call
end
9
  • I don't understand,why there is a need of lamda. Sep 21, 2013 at 18:25
  • Ahhh, I see. I was pretty sure it was calling each method as the hash was being populated, but forgot exactly why. Variables and objects, as usual :) Thanks!
    – ivan
    Sep 21, 2013 at 18:26
  • @SergioTulentsev I don't understand,why need lambda ? Without lambda it works..:) Sep 21, 2013 at 18:29
  • @Arup Rakshit - it works either way in terms of the value returned, but with the lambda it only calls the desired method of Pet. This might be important if the methods did anything other than return a value. Sep 21, 2013 at 18:57
  • 1
    @ArupRakshit If class Pet contains def feed; print "feed "; end and def put_to_bed; puts "put_to_bed"; end, the above procs are changed to pet.feed and pet.put_to_bed and .call is removed, then dispatch('put_to_bed', Pet.new) => feed put_to_bed. I'm sure you know that, so I suspect there may be a misunderstanding about what OP was saying. Sep 21, 2013 at 19:12
1

While creation of the hash all methods are evaluated. I advise to read about the use of the method send (http://www.ruby-doc.org/core-2.0.0/Object.html#method-i-send) If you use send you will end with something like:

def dispatch(command, pet)
  pet.send(command)
end

Note that you will need to adjust the name of the method putToBed.

3
  • I'm afraid, send won't do for OP. If you review sample code, you'll see that he is trying to use string "put to bed" to call a method. That string is no valid method name. Apparently, this is part of some kind of human-friendly interface. Sep 21, 2013 at 18:47
  • It would be easy to transfer "put to bed" into "put_to_bed" or "putToBed" (what btw is a bad method name), I think... Sep 21, 2013 at 19:00
  • But what about "go to sleep" => pet.put_to_bed? How do you transform that? :) That's why it's good to have mappings. So that you don't leak your implementation details to the user. Sep 21, 2013 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.