0Pricing
Ruby Academy · Lesson

prepend and Method Resolution

Ancestor chains.

prepend and Method Resolution is a free Ruby Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

include Sits Below the Class

An included module goes just below the class in the chain. The class's own methods take priority over the module's.

module M; end
class C
  include M
end
p C.ancestors  # [C, M, Object, ...]

Meet prepend

prepend inserts a module above the class in the chain. Now the module's methods are found first, before the class's own.

module M
  def hello; "from module"; end
end
class C
  prepend M
  def hello; "from class"; end
end
puts C.new.hello

prepend in the Chain

Inspect the ancestors to see prepend place the module before the class.

module M; end
class C
  prepend M
end
p C.ancestors  # [M, C, Object, ...]

super from a Prepended Module

Because the prepended module runs first, it can wrap the class method by calling super to reach the original. This is perfect for decorating behavior.

module Logged
  def save
    puts "before save"
    result = super
    puts "after save"
    result
  end
end
class Record
  prepend Logged
  def save
    puts "saving record"
    :ok
  end
end
p Record.new.save

super in Mixins

super follows the ancestor chain. From an included module, super reaches the superclass; from a prepended module, it reaches the class itself.

module Polite
  def greet; "Hi! " + super; end
end
class Person
  prepend Polite
  def greet; "I am here."; end
end
puts Person.new.greet

Wrapping Without Editing

prepend lets you add behavior around existing methods without modifying the original class code, a clean alternative to monkey-patching.

module Timing
  def compute(n)
    puts "start"
    super
  end
end
class Calc
  prepend Timing
  def compute(n); n * n; end
end
puts Calc.new.compute(5)

Multiple Modules Order

When several modules are included, the last one included sits closest to the class and is searched first.

module A; def who; "A"; end; end
module B; def who; "B"; end; end
class C
  include A
  include B
end
puts C.new.who  # B wins (included last)
p C.ancestors

include vs prepend Summary

Side by side:

  • include M: chain is [Class, M, ...] — class methods win.
  • prepend M: chain is [M, Class, ...] — module methods win and can super into the class.
module M; end
class Inc; include M; end
class Pre; prepend M; end
p Inc.ancestors
p Pre.ancestors

Putting It Together

Method resolution follows the ancestor chain:

  • Class methods beat included modules.
  • Prepended modules beat the class.
  • super walks one step up the chain.
module Cap
  def name; super.capitalize; end
end
class User
  prepend Cap
  def name; "ada"; end
end
puts User.new.name

Quick Check

Test your understanding of method resolution.

Recap

You learned method resolution:

  • Ruby searches the ancestor chain top to bottom.
  • include sits below the class; prepend sits above it.
  • super reaches the next definition up the chain.
  • prepend wraps methods cleanly.
module Loud
  def speak; super.upcase; end
end
class Bot
  prepend Loud
  def speak; "beep"; end
end
puts Bot.new.speak

Frequently asked questions

Is the “prepend and Method Resolution” lesson free?

Yes — the full text of “prepend and Method Resolution” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “prepend and Method Resolution”?

Ancestor chains. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “prepend and Method Resolution” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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