method name resolution

For the method invocation expression o.m, Ruby performs name resolution with the following steps: 1) first, it checks the eigenclass of o for singleton methods named m. 2) If no method m is found in the eigenclass, Ruby searches the class of the o for an instance method named m. 3) If no method m is found in the class, Ruby searches the instance methods of any modules included by the class of o. If that class includes more than one module, then they are searched in the reveerse of the order in which they wre included. That is, the most recently included module is searched first. If no instance method m is found in the class of o or in its modules, then the search moves up the inheirtance hierarchy to the superclass. Steps 2 and 3 are repeated for each class in the inheritance heirarchy until each ancestor class and its included modules have been searched. 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.

Notice the part "Steps 2 and 3 are repeated for each class in the inheritance heirarchy..."

What if the superclass has an eigenclass method? Why doesn't it repeat Step 1?

The answer is that when Ruby searches for singleton methods in the eigenclass of an object, it also searches the superclass (and all ancestors) of the eigenclass as well.

John Merlino wrote in post #1077564:

For the method invocation expression o.m, Ruby performs name resolution with the following steps: 1) first, it checks the eigenclass of o for singleton methods named m. 2) If no method m is found in the eigenclass, Ruby searches the class of the o for an instance method named m. 3) If no method m is found in the class, Ruby searches the instance methods of any modules included by the class of o. If that class includes more than one module, then they are searched in the reveerse of the order in which they wre included. That is, the most recently included module is searched first. If no instance method m is found in the class of o or in its modules, then the search moves up the inheirtance hierarchy to the superclass. Steps 2 and 3 are repeated for each class in the inheritance heirarchy until each ancestor class and its included modules have been searched. 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.

Notice the part "Steps 2 and 3 are repeated for each class in the inheritance heirarchy..."

What if the superclass has an eigenclass method? Why doesn't it repeat Step 1?

I'm not sure in what scenario 1) would ever be repeated. In any case, o's superclass's eigenclass is not part of o's inheritance chain:

class Animal   def self.greet     puts 'hi'   end end

class Dog < Animal end

o = Dog.new puts o.class puts o.class.superclass o.greet

--output:-- Dog Animal 1.rb:13:in `<main>': undefined method `greet' for #<Dog:0x0000010086a050> (NoMethodError)

The error output is produced by method_missing.