0Pricing
Ruby Academy · Lesson

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.ancestors

Method 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.hello

All lessons in this course

  1. Defining Modules
  2. include and extend
  3. Comparable and Enumerable
  4. prepend and Method Resolution
← Back to Ruby Academy