It inserts a module before the current class in the method lookup chain, so methods in the prepended module override the class's own methods.
Click to reveal answer
intermediate
How is prepend different from include in Ruby?
<code>include</code> adds a module after the class in the method lookup chain, so class methods override module methods. <code>prepend</code> adds the module before the class, so module methods override class methods.
Click to reveal answer
intermediate
Why use prepend for method chain insertion?
Because it lets you insert behavior before existing methods without changing the original class, enabling method wrapping or overriding cleanly.
Click to reveal answer
beginner
Given this code:<br><pre>module M
def greet
"Hello from M"
end
end
class C
def greet
"Hello from C"
end
end
c = C.new
puts c.greet</pre><br>What will <code>c.greet</code> print after <code>C.prepend(M)</code>?
It will print "Hello from M" because prepend puts module M before class C in the method lookup chain.
Click to reveal answer
intermediate
How can you call the original method from a prepended module's method?
Use <code>super</code> inside the prepended module's method to call the original method defined in the class or other modules further down the chain.
Click to reveal answer
What happens when you use prepend with a module in Ruby?
AThe class's methods override the module's methods.
BThe module's methods are ignored.
CThe module's methods override the class's methods.
DThe module is added after the class in the lookup chain.
✗ Incorrect
prepend inserts the module before the class in the method lookup chain, so the module's methods override the class's methods.
Which keyword lets you call the original method from inside a prepended module's method?
Abase
Bthis
Cself
Dsuper
✗ Incorrect
super calls the next method in the lookup chain, allowing access to the original method.
If a module is included instead of prepended, which methods take precedence?
AClass methods override module methods.
BModule methods and class methods have equal precedence.
CModule methods override class methods.
DNeither methods are called.
✗ Incorrect
include adds the module after the class, so class methods override module methods.
Why might you use prepend instead of reopening a class to override a method?
ABecause <code>prepend</code> is slower.
BTo avoid changing the original class directly and keep code modular.
module A
def info
"A" + super
end
end
module B
def info
"B" + super
end
end
class Base
def info
"Base"
end
end
class MyClass < Base
prepend A
prepend B
end
puts MyClass.new.info
hard
A. "BABase"
B. "BaseAB"
C. "ABBase"
D. "BASuperBase"
Solution
Step 1: Understand multiple prepends order
Modules prepended last are checked first, so B is before A in lookup.
Step 2: Trace method calls
B#info runs first, returns "B" + super, which calls A#info, returning "A" + super, which calls Base#info returning "Base".
Step 3: Combine results
Concatenate strings: "B" + "A" + "Base" = "BABase".
Final Answer:
"BABase" -> Option A
Quick Check:
Last prepend runs first; super chains down [OK]
Hint: Last prepended module runs first in method chain [OK]
Common Mistakes:
Assuming first prepended module runs first
Ignoring method lookup order with multiple prepends
Confusing prepend with include order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.