The object model and method lookup

ancestors

ancestors is the method lookup order, printed as a list. The first entry is the class itself, then the modules it includes, then its superclass and so on:

ruby -e 'p String.ancestors.first(5)'

[String, Comparable, Object, Kernel, BasicObject]

A call on a string is looked up in String, then Comparable, then Object. That is why < works on a string: it comes from Comparable, which uses the <=> that String defines.

prepend

include puts a module after the class; prepend puts it before, so the module can wrap the class's own method and call super into it:

ruby -e 'module M; def hi = "M"; end; class A; def hi = "A"; prepend M; end; p A.ancestors.first(3); p A.new.hi'

[M, A, Object] "M"

The module wins because it is first in the list. A prepended module that calls super gets the class's version, which is the mechanism behind a decorator that does not replace the method.

Singleton classes

A method defined on one object lives in that object's singleton class, which sits in front of its real class for that one object only:

ruby -e 'class A; def hi = "A"; end; o = A.new; def o.hi = "single"; p o.hi; p A.new.hi'

"single" "A"

o.singleton_class shows the class, and o.singleton_class.ancestors puts it before A. A class method is the same idea one level up: A is an object, and def self.x defines a method on its singleton class.

The lookup path

A call on an object goes through these steps, and knowing the order answers most "why did this method run?" questions:

  1. The object's singleton class, if it has methods.
  2. The class itself.
  3. Each prepended module, in reverse order of prepending.
  4. Each included module, in reverse order of inclusion.
  5. The superclass chain, each with its own prepends and includes.
  6. method_missing, if nothing matched.

obj.method(:hi).owner answers which class or module supplied the method, which is faster than reading the list when a framework has added three layers.

What to watch for

Adding a module with include twice is harmless; prepending it to a class and again to its subclass puts it in the chain twice, and super calls it twice. Guard a prepend with a check or make the module idempotent.

super without arguments forwards the same arguments; super() passes none. The difference is the first thing to check when a wrapper changes a method's behaviour.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does String.ancestors.first(5) show? p String.ancestors.first(5)

    Answer

    [String, Comparable, Object, Kernel, BasicObject]

    The class comes first, then the modules it includes, then the superclass chain.

  2. What does this program print? module M def hi = "M" end class A def hi = "A" prepend M end p A.ancestors.first(3) p A.new.hi

    Answer

    [M, A, Object] / "M"

    A prepended module sits in front of the class, so it wins the lookup.

  3. What does this program print? module M def hi = "M>#{super}" end class A def hi = "A" prepend M end puts A.new.hi

    Answer

    M>A

    The prepended module wraps the class method and calls it through super, which is the decorator shape.

  4. What does this program print? class A def hi = "A" end o = A.new def o.hi = "single" p o.hi p A.new.hi

    Answer

    "single" / "A"

    The singleton method belongs to one object, and a second instance still uses the class method.

  5. Where does include put a module relative to the class?

    Answer

    After the class in the lookup order

    An included module is searched after the class, while prepend puts it in front.

  6. What is a class method such as def self.x?

    Answer

    A method on the class object's singleton class

    A class is an object too, so a class method is a singleton method one level up.

  7. What does this program print? class A def hi = "A" end puts A.new.method(:hi).owner

    Answer

    A

    owner names the class or module that supplied the method, which is faster than reading the ancestor list.

  8. What does super() pass that super does not?

    Answer

    Nothing: super forwards the same arguments and super() passes none

    The empty parentheses are the difference, and it is the first thing to check when a wrapper behaves oddly.

  9. Which entry of the lookup path comes before the class itself?

    Answer

    The object's singleton class

    A method defined on one object is found before the class it belongs to.

  10. What happens when the same module is prepended to a class and to its subclass?

    Answer

    It appears twice in the subclass chain, and super calls it twice

    Each class has its own prepend, so the subclass chain holds the module twice and the wrapper runs twice.

Article author: Arthur Isaev