Prepend lets you add a module's methods before others in a class. This helps change or add behavior without changing original code.
Prepend for method chain insertion in Ruby
module YourModule def method_name # new behavior super # calls original method end end class YourClass prepend YourModule end
Use prepend inside the class to insert the module before the class in method lookup.
Calling super inside the module method runs the original method.
hello method.module Greeting def hello puts "Hi!" # new behavior super # call original end end class Person def hello puts "Hello from Person" end prepend Greeting end Person.new.hello
module Logger def save puts "Logging before save" super end end class Record def save puts "Saving record" end prepend Logger end Record.new.save
This program shows how prepend inserts Announcer before Speaker. The speak method in Announcer runs first, then calls the original speak.
module Announcer def speak puts "Starting to speak..." super end end class Speaker def speak puts "Hello, world!" end prepend Announcer end Speaker.new.speak
Prepend changes the order Ruby looks for methods, putting the module first.
Use super in the module method to run the original method after your added code.
Prepend is useful for adding behavior without rewriting or copying code.
Prepend inserts a module before a class in method lookup.
It lets you add or change behavior by running module methods first.
Use super in the module to call the original method.
Practice
prepend keyword do in Ruby when used with a module and a class?Solution
Step 1: Understand method lookup order with prepend
Usingprependplaces the module before the class in the method lookup chain, so module methods are checked first.Step 2: Compare prepend with other module inclusion methods
Unlikeincludewhich adds the module after the class,prependinserts it before, changing method resolution order.Final Answer:
It inserts the module before the class in the method lookup chain. -> Option AQuick Check:
Prepend = module methods run first [OK]
- Confusing prepend with include
- Thinking prepend replaces class methods permanently
- Assuming prepend appends module after class
Mod to a class MyClass?Solution
Step 1: Recall Ruby syntax for prepend
The correct syntax to prepend a module isprepend ModuleNameinside the class definition.Step 2: Eliminate other options
includeadds module after class,extendadds methods as class methods, andinsertis invalid syntax.Final Answer:
class MyClass; prepend Mod; end -> Option CQuick Check:
Prepend syntax = prepend ModuleName [OK]
- Using include instead of prepend
- Using extend which affects class methods
- Typing invalid keywords like insert
module M
def greet
"Hello from M, " + super
end
end
class C
prepend M
def greet
"Hello from C"
end
end
puts C.new.greetSolution
Step 1: Understand method lookup with prepend
BecauseMis prepended, itsgreetmethod is called first.Step 2: Follow the super call in module method
ThesuperinsideM#greetcalls the originalgreetinC, returning "Hello from C".Step 3: Combine returned strings
The module method returns "Hello from M, " + "Hello from C" resulting in "Hello from M, Hello from C".Final Answer:
"Hello from M, Hello from C" -> Option BQuick Check:
Prepend + super calls original method [OK]
- Ignoring super call and expecting only module output
- Thinking prepend replaces original method without super
- Expecting NoMethodError due to super
module M
def hello
super + " world"
end
end
class C
prepend M
end
puts C.new.helloSolution
Step 1: Check method definitions
The moduleMdefineshellocallingsuper, but classChas nohellomethod.Step 2: Understand super call behavior
SinceChas nohello,superhas no method to call, causing aNoMethodError.Final Answer:
NoMethodError because super has no method to call -> Option DQuick Check:
Super needs original method; missing causes NoMethodError [OK]
- Assuming super works without original method
- Thinking prepend fixes missing methods automatically
- Confusing syntax error with runtime error
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.infoSolution
Step 1: Understand multiple prepends order
Modules prepended last are checked first, soBis beforeAin lookup.Step 2: Trace method calls
B#inforuns first, returns "B" + super, which callsA#info, returning "A" + super, which callsBase#inforeturning "Base".Step 3: Combine results
Concatenate strings: "B" + "A" + "Base" = "BABase".Final Answer:
"BABase" -> Option AQuick Check:
Last prepend runs first; super chains down [OK]
- Assuming first prepended module runs first
- Ignoring method lookup order with multiple prepends
- Confusing prepend with include order
Ready to prove it?
Pass the test and this pattern moves unknown
