Bird
Raised Fist0
Rubyprogramming~20 mins

Prepend for method chain insertion in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Prepend Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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
A"Hello from M, Hello from MyClass"
B"Hello from MyClass"
CNoMethodError
D"Hello from M"
Attempts:
2 left
💡 Hint
Remember that prepend inserts the module before the class in the method lookup chain.
❓ Predict Output
intermediate
2: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
A"ABBase"
B"BaseAB"
C"BABase"
D"BaseBA"
Attempts:
2 left
💡 Hint
Modules prepended later are placed before earlier ones in the lookup chain.
🔧 Debug
advanced
2: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
AThe class method <code>greet</code> is missing a <code>super</code> call causing infinite recursion.
BThe <code>greet</code> method in module <code>M</code> calls itself recursively without calling <code>super</code>.
CThe module <code>M</code> is not properly prepended before the class.
DThe <code>greet</code> method in <code>MyClass</code> is private and cannot be called.
Attempts:
2 left
💡 Hint
Look at how greet in M calls greet again instead of super.
📝 Syntax
advanced
2: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?
A
class MyClass
  prepend M
  def hello
    "Hello from MyClass"
  end
end
B
class MyClass
  def hello
    "Hello from MyClass"
  end
  prepend M
end
C
module M
  def hello
    "Hello from M"
  end
end
class MyClass
  def hello
    "Hello from MyClass"
  end
end
MyClass.include(M)
D
class MyClass
  def hello
    "Hello from MyClass"
  end
end
M.prepend(MyClass)
Attempts:
2 left
💡 Hint
The prepend call must be inside the class body before method definitions or after, but not after class end.
🚀 Application
expert
3: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
A
module Logger
  def add(a, b)
    puts "Adding #{a} and #{b}"
  end
end
Calculator.prepend(Logger)
B
module Logger
  def add(a, b)
    puts "Adding #{a} and #{b}"
    add(a, b)
  end
end
Calculator.prepend(Logger)
C
module Logger
  def add(a, b)
    puts "Adding #{a} and #{b}"
    Calculator.new.add(a, b)
  end
end
Calculator.prepend(Logger)
D
module Logger
  def add(a, b)
    puts "Adding #{a} and #{b}"
    super
  end
end
Calculator.prepend(Logger)
Attempts:
2 left
💡 Hint
Use super to call the original method after logging.

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

  1. 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.
  2. 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.
  3. Final Answer:

    It inserts the module before the class in the method lookup chain. -> Option A
  4. 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

  1. Step 1: Recall Ruby syntax for prepend

    The correct syntax to prepend a module is prepend ModuleName inside the class definition.
  2. Step 2: Eliminate other options

    include adds module after class, extend adds methods as class methods, and insert is invalid syntax.
  3. Final Answer:

    class MyClass; prepend Mod; end -> Option C
  4. 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

  1. Step 1: Understand method lookup with prepend

    Because M is prepended, its greet method is called first.
  2. Step 2: Follow the super call in module method

    The super inside M#greet calls the original greet in C, returning "Hello from C".
  3. Step 3: Combine returned strings

    The module method returns "Hello from M, " + "Hello from C" resulting in "Hello from M, Hello from C".
  4. Final Answer:

    "Hello from M, Hello from C" -> Option B
  5. 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

  1. Step 1: Check method definitions

    The module M defines hello calling super, but class C has no hello method.
  2. Step 2: Understand super call behavior

    Since C has no hello, super has no method to call, causing a NoMethodError.
  3. Final Answer:

    NoMethodError because super has no method to call -> Option D
  4. Quick Check:

    Super needs original method; missing causes NoMethodError [OK]
Hint: Super must call existing method; else NoMethodError [OK]
Common Mistakes:
  • Assuming super works without original method
  • Thinking prepend fixes missing methods automatically
  • Confusing syntax error with runtime error
5. Given this code, what will be the output?
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

  1. Step 1: Understand multiple prepends order

    Modules prepended last are checked first, so B is before A in lookup.
  2. 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".
  3. Step 3: Combine results

    Concatenate strings: "B" + "A" + "Base" = "BABase".
  4. Final Answer:

    "BABase" -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard