1.1 Method Missing

Introducing #method_missing

We need you to do some spying. We need you to create an object which will accept any messages passed to it. And something that won't reveal any information about itself. Now, let's see how that would work.

Example Code:

Output Window

That's no good is it? You'll be found out immediately. "But, hold on", you say. "You said it should accept any messages passed to it. And you're using method calls. I'm confused."

Well, what you need to understand is any method call in Ruby is actually a message being passed to that object. One way to demonstate is to look at the method_missing method. Let's take a look at it in it's most basic form and see if we can use it to solve our previous problem. You need to define a method in your class which accepts one argument (maybe called sym) which will have the name of the method that was called on your object (as a symbol).

Hint

Maybe create that "method_missing" method!

Output Window

Passing Arguments

Excellent comrade! They will never catch us now! Drat, what if they pass some arguments with those messages? You'll fail at that point, won't you? If only there was a way to handle any arguments that get passed to you as well. Luckily there is. method_missing could potentially accept a second argument which will be an array of arguments that was passed into the original missing method. Let's see if you can trick the enemy now.

Hint

method_missing can take more than one argument.

Output Window

Blocks

We're almost there. But, sources in the enemy camp tell me that they will be passing you a block to execute on some of these message. And you will be required to invoke this block (if you're passed on) with the same arguments you were passed in.

Ruby gives you the option to accept yet another argument to the method_missing method, &block. Therefore your signature now looks like method_missing(sym, *args, &block).

Hint

Maybe missing_method takes yet another argument.

Output Window

Perfect! They'll never catch on. All we need you to do is to come back and report to us on what they told you.

Hint

Perhaps saving the calls that come through will be helpful. An array is a particularly useful data structure to hold the information of multiple calls.

Output Window

Send

Excellent news comrade! We've just made your job much easier. We have captured an enemy agent. Any message they send to you, we want you to send the same on to the captured enemy agent and report the response back, while still recording what they asked you, of course. And how do you send a message to this enemy agent? Well, using send, of course. Every object in Ruby defines a send method. The first argument you pass to it is the method you wish to call, and the arguments after that are the arguments you wish to pass to the method.

Hint

Perhaps you want to send a call out to the @enemy_agent object by using send. Remember, the signature of send is @enemy_agent.send(:method_name, arg1, arg2...).

Output Window

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2014 by Sidu Ponnappa and Srushti