Method missing is a very powerful and widely used Ruby functionality in Rails. We can over-write existing methods. Now when we make a call to that method, the method name is first looked up in the present class and then the parent class. As we are using method missing and lets say we have the function declaration in the present class then the method is found there itself. Thus no need to search for the methods in the parent class and so on. Wouldn't this make execution faster? If so, is it advisable to just over-write every common inbuilt Ruby methods we are using to make execution faster?
Not really. In fact, method_missing() is generally slower than a regular method.
Here's what happens: when you call a method that doesn't exist, Ruby looks for it in the receiving object's class first. If it doesn't find the method there, it looks for it in the superclass - and then the superclass and the superclass, and so on, until it eventually reaches the top of the chain (Object in Ruby 1.8, BasicObject in Ruby 1.9). Only then, if it still didn't find the method, does Ruby get down in the receiver's class again to look for a method_missing(). I described the process in detail in Metaprogramming Ruby.
So the reason to use method_missing() is not to make your code faster. It's a design spell, not a performance spell.
However, in general, my advice is not to worry about this kind of performance issues too early. Calling methods is generally much faster than doing a lot of other things. My mantra is: first make it work, then make it right, and only then (armed with unit tests and a profiler) worry about making it faster.
Paolo Perrotta, Europe
Author of Metaprogramming Ruby
method_missing really doesn't get used all that much in Ruby. At the very least it should be extremely rare for an average developer to define one.
Often when method_missing is implemented, the method_missing will turn around and define the method you tried to call so that, when you invoke the method again, it exists on the object and you don't have to take the performance hit of fallng through the class hierarchy and then into method_missing again.
A good workman is known by his tools.
Legend has it that if you rub the right tiny ad, a genie comes out.
Two software engineers solve most of the world's problems in one K&R sized book