Ruby modules: prepend vs include
This course looks at how Ruby decides which method body runs when a class and a module define the same name. It uses .ancestors to show that include places a module after the class and prepend places it before. Lessons trace calls with puts, follow super one step along the chain, combine both keywords on one class, and explain the NoMethodError raised when super finds nothing. The last lesson compares when each keyword fits, including wrapping a method without editing the class.
What you'll learn
- ✓How .ancestors sets the order of Ruby's method lookup
- ✓Where include and prepend place a module relative to the class
- ✓Using puts traces to see which method bodies run
- ✓How super moves one step along .ancestors
- ✓Combining include and prepend on one class
- ✓Choosing include for shared behavior and prepend for wrapping a method
Recap: include and the ancestor chain
In Ruby modules: include/extend you wrote include SomeModule inside a class body to mix a module's methods into every instance of the class. This course doesn't reteach that. It answers a sharper question the earlier course only touched on: when Ruby looks for a method, in what order does it check…
Read the full lesson free →Curriculum
7 lessonsRecap: include and the ancestor chain
Recaps how include places a module after the class, so the class's own method wins.
prepend inserts a module before the class
Shows that prepend puts the module before the class, so its method runs first and controls the call.
Ancestors, side by side: include vs prepend
Compares both ancestor arrays and shows the NoMethodError when a prepended module's super finds nothing.
Proving which method runs first
Uses puts traces to show include replaces the module's method while prepend wraps the class's method.
super inside a prepended module reaches the class
Explains that super inside a prepended module moves one step along .ancestors and reaches the class.
Combining include and prepend on the same class
Applies include and prepend to one class and follows each super call through the resulting chain.
Putting it together: choosing include or prepend
Reviews the placement rule and compares when include fits and when prepend fits for wrapping methods.