prepend and Method Resolution
Ancestor chains.
Ancestor Chains
When you call a method, Ruby searches a list called the ancestor chain. The first matching definition wins. Understanding this chain explains how mixins and inheritance interact.
class Animal; end
class Dog < Animal; end
p Dog.ancestorsMethod Lookup Order
Ruby looks in the object's class first, then its included modules, then its superclass, and so on up the chain until it finds the method.
module M
def hello; "from module"; end
end
class C
include M
def hello; "from class"; end
end
puts C.new.helloAll lessons in this course
- Defining Modules
- include and extend
- Comparable and Enumerable
- prepend and Method Resolution