aspose file tools*
The moose likes Ruby and the fly likes Method missing in Ruby. Does it make execution of functions in a Rails app any faster. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Ruby
Bookmark "Method missing in Ruby. Does it make execution of functions in a Rails app any faster." Watch "Method missing in Ruby. Does it make execution of functions in a Rails app any faster." New topic
Author

Method missing in Ruby. Does it make execution of functions in a Rails app any faster.

a swain
Greenhorn

Joined: Mar 29, 2010
Posts: 1
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?
Paolo Perrotta
author
Greenhorn

Joined: Mar 27, 2010
Posts: 27
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
http://www.pragprog.com/titles/ppmetr/metaprogramming-ruby
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Method missing in Ruby. Does it make execution of functions in a Rails app any faster.
 
Similar Threads
Is there anything close to Active Record for Java?
Ruby Best Practices: large teams, performance, deployment, and more...
What advantages does Ruby hold over PHP and Java?
Ruby presence in the current market
Ruby in Practice - Ruby as a first choice