Ruby method lookup(method name resolution) algorithm

1Milinda10th Aug 2008ruby

I found this nice discussion(http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/33615) about Ruby's name resolution algorithm while browsing through the internet. You can browse through the discussion by clicking 'N' button of the tool bar on top of that page. From the 'Ruby Programming Language' book, I found following algorithm for method lookup.For example take method invocation expression o.m, Ruby goes through the folliwng step for the name resolution:
  1. As the first step, Ruby checks the eigenclass of o for singleton method named m.
  2. If the method named m is not found in the eigenclass, Ruby search the class of o for an instance method name m.
  3. If the method m is not found in the class, Ruby searches the instance methods of any modules included by the class of o. If there are modules included, they searched in the reverse of the order which they are included.
  4. If no instance method m is found in the class of o or in its modules, then the search moves up the inheritance hierarchy of the super class. Step 2 and 3 are repeated for each class in the inheritance hierarchy until each ancestor class and its included modules have been searched.
  5. If no method named m is found after completing the search, then a method named method_missing is invoked instead. In order to find an appropriate definition of this method, the name resolution algorithm starts over at step 1. The Kernel module provides a default implementation of method_missing, so this second pass of name resolution is guaranteed to succeed.
Let's look at simple example to understand this algorithm:
test_str = "hello"
test_str.world
In the above code we want to invoke a method named world on the String instance "hello". Name resolution proceeds as follows:
  1. Check the eigenclass for singleton methods. There aren't any in this case.
  2. Check the String class. There is no instance method named world.
  3. Check the Comparable and Enumerable modules of the String class for an instance method named world. Neither module defines such a method.
  4. Check the superclass of String, which is Object. The Object class does not define a method named world, either.
  5. Check the Kernel module included by Object. The world method is not found here either, so we now switch to looking for a method named method_missing.
  6. Look for method_missing in each of the spots above (the eigenclass of the String object, the String class, the Comparable and Enumerable modules, the Object class, and the Kernel module). The first definition of method_missing we find is in the Kernel module, so this is the method we invoke. What it does is raise an exception: undefined method `hello' for "Hello":String (NoMethodError)
Here is the example from the discussion I mentioned at the beginning of the post:
class Class
   def m
      p "Class#m"
   end
end
 
class A
end
 
class B < A
end
 
B.m	# Prints out "Class#m"
 
def A.m
   p "A.m"
end
 
B.m	# Prints out "A.m"
By examine the above code you can understand the algorithm I discussed earlier in the post. It clearly shows how method names are resolve through the inheritance heirarchy. Related posts: Sphere: Related Content

Related posts brought to you by Yet Another Related Posts Plugin.

1 Comment Comments Feed

  1. coderrr (August 26, 2008, 12:27 pm).

    check out my post on how constant name resolution in ruby works: http://coderrr.wordpress.com/2008/03/11/constant-name-resolution-in-ruby/

Add a Comment