0
0
Rubyprogramming~20 mins

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

Choose your learning style9 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.