Challenge - 5 Problems
Ruby Prepend Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Ruby code using prepend?
Consider the following Ruby code that uses
prepend to insert a module into a class method chain. What will be printed when MyClass.new.greet is called?Ruby
module M def greet "Hello from M, " + super end end class MyClass def greet "Hello from MyClass" end end MyClass.prepend(M) puts MyClass.new.greet
Attempts:
2 left
💡 Hint
Remember that
prepend inserts the module before the class in the method lookup chain.✗ Incorrect
Using
prepend, the module M is placed before MyClass in the method lookup chain. So when greet is called, Ruby first calls M#greet, which calls super to invoke MyClass#greet. The outputs concatenate, resulting in "Hello from M, Hello from MyClass".❓ Predict Output
intermediate2:00remaining
What does this code output when using prepend with multiple modules?
Given the following Ruby code, what will be the output of
obj.call?Ruby
module A def call "A" + super end end module B def call "B" + super end end class Base def call "Base" end end class MyClass < Base prepend A prepend B end obj = MyClass.new puts obj.call
Attempts:
2 left
💡 Hint
Modules prepended later are placed before earlier ones in the lookup chain.
✗ Incorrect
The last prepended module (
B) is checked first, then A, then Base. So the call chain is B#call → A#call → Base#call. Each adds its letter before calling super, resulting in "BABase".🔧 Debug
advanced2:00remaining
Why does this prepend example cause a SystemStackError?
Examine the code below. When
MyClass.new.greet is called, it causes a SystemStackError (stack level too deep). Why does this happen?Ruby
module M def greet "Hi " + greet end end class MyClass prepend M def greet "there" end end MyClass.new.greet
Attempts:
2 left
💡 Hint
Look at how
greet in M calls greet again instead of super.✗ Incorrect
The
greet method in module M calls itself recursively by calling greet instead of super. This causes infinite recursion and eventually a SystemStackError.📝 Syntax
advanced2:00remaining
Which option correctly prepends a module to insert behavior before a method?
You want to prepend module
M to class MyClass so that M#hello runs before MyClass#hello. Which code snippet correctly does this?Attempts:
2 left
💡 Hint
The
prepend call must be inside the class body before method definitions or after, but not after class end.✗ Incorrect
Option A correctly prepends module
M inside the class body before defining hello. This ensures M#hello is called before MyClass#hello. Option A is valid syntax in Ruby and will work correctly; prepend can appear after method definitions inside the class body. Option A tries to prepend a class to a module, which is invalid. Option A uses include instead of prepend, so M#hello would be called after MyClass#hello.🚀 Application
expert3:00remaining
How to use prepend to log method calls without changing original class?
You have a class
Calculator with a method add(a, b). You want to log every call to add without modifying Calculator directly. Which module and prepend usage correctly logs the call and then calls the original method?Ruby
class Calculator def add(a, b) a + b end end
Attempts:
2 left
💡 Hint
Use
super to call the original method after logging.✗ Incorrect
Option D correctly defines
Logger#add that logs the arguments and then calls super to invoke the original Calculator#add. This way, the original method is called without modifying Calculator. Option D causes infinite recursion by calling add inside add. Option D creates a new instance and calls add on it, which is inefficient and breaks method chain. Option D logs but does not call the original method, so it changes behavior.