The method lookup chain shows how Ruby finds the code to run when you call a method on an object. It helps understand which method runs if many have the same name.
Method lookup chain in Ruby
object.method(:method_name).owner
This code shows which class or module owns the method Ruby will run.
You can also use Module#ancestors to see the full lookup chain.
hello method called on b is owned by class B, because B overrides A's method.class A def hello puts 'Hello from A' end end class B < A def hello puts 'Hello from B' end end b = B.new p b.method(:hello).owner
greet method comes from module M included in class C.module M def greet puts 'Hi from M' end end class C include M end c = C.new p c.method(:greet).owner
to_s is owned by Ruby's Object class, because D does not define it.class D end d = D.new p d.method(:to_s).owner
This program shows which speak method runs when called on child. It also prints the owner of the method and the full method lookup chain for class Child.
module M1 def speak puts 'Speak from M1' end end module M2 def speak puts 'Speak from M2' end end class Parent def speak puts 'Speak from Parent' end end class Child < Parent include M1 include M2 end child = Child.new child.speak p child.method(:speak).owner p Child.ancestors
The method lookup chain starts from the object's class, then checks included modules in reverse order of inclusion, then parent classes.
Modules included later override methods from modules included earlier.
You can use ancestors to see the full chain Ruby uses to find methods.
The method lookup chain shows where Ruby finds a method to run.
It helps understand method overriding and module inclusion order.
Use object.method(:name).owner and Class.ancestors to explore it.