# More misleading content on the web regarding ruby method lookup algorithm

**URL:** <https://www.ruby-forum.com/t/more-misleading-content-on-the-web-regarding-ruby-method-lookup-algorithm/223494>\
**Category:** Rails\
**Created:** [October 6, 2012, 9:44pm UTC](https://www.ruby-forum.com/t/more-misleading-content-on-the-web-regarding-ruby-method-lookup-algorithm/223494 "2012-10-06T21:44:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [October 6, 2012, 9:44pm UTC](https://www.ruby-forum.com/t/more-misleading-content-on-the-web-regarding-ruby-method-lookup-algorithm/223494/1 "2012-10-06T21:44:54Z")

</div>

I read this article:

[http://www.madebydna.com/all/code/2011/06/24/eigenclasses-demystified.html](http://www.madebydna.com/all/code/2011/06/24/eigenclasses-demystified.html)

Author makes claim that are flat out wrong. She says: “Class gets  
“pushed up” the lookup chain and becomes a superclass.” That statement  
is flat out wrong. A class object does not inherit from class Class.  
In fact, if you try it you will get a TypeError. A class object is an  
instance of class Class. If a class object does not inherit from  
anything, then it implicitly inherits from class Object, which itself  
is an instance of class Class. What happens is as follows: First off,  
when a class (aka singleton) method is defined on class Dog, an  
eigenclass is created. For all intents and purposes, this eigenclass  
becomes the class of the Dog object. Class does not get “pushed up”  
the lookup chain. It’s the superclass that gets pushed up the lookup  
chain, which in this example, is Mammal. In other words, it will look  
at Dog’s eigenclass and if it doesn’t find it there, it will then look  
at Mammal’s eigenclass, and then Object (so its Object that  
continuously gets pushed back) and then BasicObject. Here’s an example  
that shows Class does not get pushed back, because the class object  
never even inherits the eigenclass of Class to begin with (the class  
object is simply an instance of Class):

Loading development environment (Rails 3.1.2)  
1.9.3p0 :001 \> class Mammal  
1.9.3p0 :002?\> def Mammal.mammal\_singleton  
1.9.3p0 :003?\> puts ‘mammal singleton’  
1.9.3p0 :004?\> end  
1.9.3p0 :005?\> end  
=\> nil  
1.9.3p0 :006 \>  
1.9.3p0 :007 \> class Dog \< Mammal  
1.9.3p0 :008?\> def Dog.dog\_singleton  
1.9.3p0 :009?\> puts ‘dog singleton’  
1.9.3p0 :010?\> end  
1.9.3p0 :011?\> end  
=\> nil  
1.9.3p0 :012 \>  
1.9.3p0 :013 \> class Object  
1.9.3p0 :014?\> def Object.object\_singleton  
1.9.3p0 :015?\> puts ‘object singleton’  
1.9.3p0 :016?\> end  
1.9.3p0 :017?\> end  
=\> nil  
1.9.3p0 :018 \>  
1.9.3p0 :019 \> class Class  
1.9.3p0 :020?\> def Class.class\_singleton  
1.9.3p0 :021?\> puts ‘class singleton’  
1.9.3p0 :022?\> end  
1.9.3p0 :023?\> end  
=\> nil  
1.9.3p0 :024 \> Dog.dog\_singleton  
dog singleton  
=\> nil  
1.9.3p0 :025 \> Dog.mammal\_singleton  
mammal singleton  
=\> nil  
1.9.3p0 :026 \> Dog.object\_singleton  
object singleton  
=\> nil  
1.9.3p0 :027 \> Dog.class\_singleton  
NoMethodError: undefined method `class\_singleton’ for Dog:Class  
from (irb):27

#It fails because class Class is not in the eigenclass lookup chain of  
Dog class object, since class Class is not a superclass of Dog.  
However, by defining an instance method in class Class, then that will  
be able to be invoked on Dog, since Dog is an instance of class Class:

1.9.3p0 :028 \> class Class  
1.9.3p0 :029?\> def class\_instance  
1.9.3p0 :030?\> puts ‘class instance’  
1.9.3p0 :031?\> end  
1.9.3p0 :032?\> end  
=\> nil  
1.9.3p0 :033 \> Dog.class\_instance  
class instance  
=\> nil

So the real question is this, which that article obscured. If I invoke  
a method on Dog, will it search the Class hierarchy for an associated  
instance method first (and continue looking up the lookup algorithm  
for instance methods via Module, Object, and BasicObject which class  
Class inherits from) or will it search Dog’s eigenclass and its  
superclasses’ eigenclasses (all the way up to the eigenclass of Object  
and BasicObject) first? The proof is in the pudding:

1.9.3p0 :044 \> class Object  
1.9.3p0 :045?\> def Object.sing\_method  
1.9.3p0 :046?\> puts ‘singleton method’  
1.9.3p0 :047?\> end  
1.9.3p0 :048?\> def inst\_method  
1.9.3p0 :049?\> puts ‘instance method’  
1.9.3p0 :050?\> end  
1.9.3p0 :051?\> end  
=\> nil  
1.9.3p0 :052 \> Dog.sing\_method  
singleton method  
=\> nil  
1.9.3p0 :053 \> Dog.inst\_method  
instance method  
=\> nil  
#as suspected, invocations on Dog lead to both eigenclass methods of  
Object and instance methods of Object, since Dog is an instance of  
class Class, and class Class inherits from Object via Module, and Dog  
at the same time inherits from Object (via inheritance), which is done  
implicitly by Ruby. Now which get invoked first, the instance methods  
or eigenclass methods?

1.9.3p0 :062 \> class Object  
1.9.3p0 :063?\> def Object.method\_lookup  
1.9.3p0 :064?\> puts ‘eigenclass method called first!’  
1.9.3p0 :065?\> end  
1.9.3p0 :066?\> def method\_lookup  
1.9.3p0 :067?\> puts ‘instance method is called first!’  
1.9.3p0 :068?\> end  
1.9.3p0 :069?\> end  
=\> nil  
1.9.3p0 :070 \> Dog.method\_lookup  
eigenclass method called first!  
=\> nil

And there you have it, the method resolution algorithm looks up the  
singleton methods prior to the instance methods of a class object.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [October 6, 2012, 10:18pm UTC](https://www.ruby-forum.com/t/more-misleading-content-on-the-web-regarding-ruby-method-lookup-algorithm/223494/2 "2012-10-06T22:18:12Z")

</div>

Hi John,

On Sat, Oct 6, 2012 at 2:43 PM, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I read this article:
> 
> [http://www.madebydna.com/all/code/2011/06/24/eigenclasses-demystified.html](http://www.madebydna.com/all/code/2011/06/24/eigenclasses-demystified.html)
> 
> Author makes claim that are flat out wrong. She says: “Class gets  
> “pushed up” the lookup chain and becomes a superclass.” That statement  
> is flat out wrong.

Nice job. Just FYI, Dave T. did a Screencast series on Ruby  
MetaProgramming, the first of which covers this very clearly. I found  
it  
well worth the $5.

Best regards,  
Bill
