This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Method missing in Ruby. Does it make execution of functions in a Rails app any faster.