📖 Scenario: Imagine you have a simple Ruby class that formats text messages. You want to add a new behavior that always adds a greeting at the start of the message, but you want to do this without changing the original class code directly.
🎯 Goal: You will learn how to use Ruby's prepend feature to insert a new method behavior at the start of an existing method chain. This lets you add a greeting before the original message is formatted.
📋 What You'll Learn
Create a class called MessageFormatter with a method format that returns a string.
Create a module called GreetingPrepender with a method format that adds a greeting before calling super.
Use prepend to insert GreetingPrepender into MessageFormatter.
Create an instance of MessageFormatter and call format to see the greeting added.
💡 Why This Matters
🌍 Real World
Using <code>prepend</code> helps developers add or change features in existing classes without editing their original code. This is useful in large projects or when using third-party libraries.
💼 Career
Understanding <code>prepend</code> is important for Ruby developers working on maintainable and extendable codebases, especially in frameworks like Rails where method chaining and module mixins are common.
Progress0 / 4 steps
1
Create the MessageFormatter class
Create a class called MessageFormatter with a method format that returns the string "This is the original message."
Ruby
Hint
Define a class with class MessageFormatter and a method def format that returns the exact string.
2
Create the GreetingPrepender module
Create a module called GreetingPrepender with a method format that returns the string "Hello! " concatenated with super.
Ruby
Hint
Define a module with module GreetingPrepender and a method format that adds "Hello! " before calling super.
3
Prepend the module to the class
Use prepend GreetingPrepender inside the MessageFormatter class to insert the module before the class methods.
Ruby
Hint
Inside the class, write prepend GreetingPrepender before the method definitions.
4
Create instance and print formatted message
Create an instance of MessageFormatter called formatter and print the result of calling formatter.format.
Ruby
Hint
Create the instance with formatter = MessageFormatter.new and print with puts formatter.format.
Practice
(1/5)
1. What does the prepend keyword do in Ruby when used with a module and a class?
easy
A. It inserts the module before the class in the method lookup chain.
B. It appends the module after the class in the method lookup chain.
C. It replaces the class methods with the module methods permanently.
D. It creates a copy of the module inside the class without affecting method lookup.
Solution
Step 1: Understand method lookup order with prepend
Using prepend places 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
Unlike include which adds the module after the class, prepend inserts it before, changing method resolution order.
Final Answer:
It inserts the module before the class in the method lookup chain. -> Option A
Quick Check:
Prepend = module methods run first [OK]
Hint: Prepend means module methods run before class methods [OK]
Common Mistakes:
Confusing prepend with include
Thinking prepend replaces class methods permanently
Assuming prepend appends module after class
2. Which of the following is the correct syntax to prepend a module Mod to a class MyClass?
easy
A. class MyClass; extend Mod; end
B. class MyClass; include Mod; end
C. class MyClass; prepend Mod; end
D. class MyClass; insert Mod; end
Solution
Step 1: Recall Ruby syntax for prepend
The correct syntax to prepend a module is prepend ModuleName inside the class definition.
Step 2: Eliminate other options
include adds module after class, extend adds methods as class methods, and insert is invalid syntax.
Final Answer:
class MyClass; prepend Mod; end -> Option C
Quick Check:
Prepend syntax = prepend ModuleName [OK]
Hint: Use 'prepend ModuleName' inside class to insert module first [OK]
Common Mistakes:
Using include instead of prepend
Using extend which affects class methods
Typing invalid keywords like insert
3. What is the output of this Ruby code?
module M
def greet
"Hello from M, " + super
end
end
class C
prepend M
def greet
"Hello from C"
end
end
puts C.new.greet
medium
A. "Hello from C"
B. "Hello from M, Hello from C"
C. NoMethodError
D. "Hello from M"
Solution
Step 1: Understand method lookup with prepend
Because M is prepended, its greet method is called first.
Step 2: Follow the super call in module method
The super inside M#greet calls the original greet in C, 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 B
Quick Check:
Prepend + super calls original method [OK]
Hint: Prepend runs module method first; super calls original method [OK]
Common Mistakes:
Ignoring super call and expecting only module output
Thinking prepend replaces original method without super
Expecting NoMethodError due to super
4. Identify the error in this Ruby code using prepend:
module M
def hello
super + " world"
end
end
class C
prepend M
end
puts C.new.hello
medium
A. TypeError because prepend is used incorrectly
B. No error; outputs " world"
C. SyntaxError due to missing method in class
D. NoMethodError because super has no method to call
Solution
Step 1: Check method definitions
The module M defines hello calling super, but class C has no hello method.
Step 2: Understand super call behavior
Since C has no hello, super has no method to call, causing a NoMethodError.
Final Answer:
NoMethodError because super has no method to call -> Option D
Quick Check:
Super needs original method; missing causes NoMethodError [OK]
Hint: Super must call existing method; else NoMethodError [OK]
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.