0
0
Rubyprogramming~5 mins

Prepend for method chain insertion in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the prepend method do in Ruby?
It inserts a module before the current class in the method lookup chain, so methods in the prepended module override the class's own methods.
Click to reveal answer
intermediate
How is prepend different from include in Ruby?
<code>include</code> adds a module after the class in the method lookup chain, so class methods override module methods. <code>prepend</code> adds the module before the class, so module methods override class methods.
Click to reveal answer
intermediate
Why use prepend for method chain insertion?
Because it lets you insert behavior before existing methods without changing the original class, enabling method wrapping or overriding cleanly.
Click to reveal answer
beginner
Given this code:<br><pre>module M
  def greet
    "Hello from M"
  end
end

class C
  def greet
    "Hello from C"
  end
end

c = C.new
puts c.greet</pre><br>What will <code>c.greet</code> print after <code>C.prepend(M)</code>?
It will print "Hello from M" because prepend puts module M before class C in the method lookup chain.
Click to reveal answer
intermediate
How can you call the original method from a prepended module's method?
Use <code>super</code> inside the prepended module's method to call the original method defined in the class or other modules further down the chain.
Click to reveal answer
What happens when you use prepend with a module in Ruby?
AThe class's methods override the module's methods.
BThe module's methods are ignored.
CThe module's methods override the class's methods.
DThe module is added after the class in the lookup chain.
Which keyword lets you call the original method from inside a prepended module's method?
Abase
Bthis
Cself
Dsuper
If a module is included instead of prepended, which methods take precedence?
AClass methods override module methods.
BModule methods and class methods have equal precedence.
CModule methods override class methods.
DNeither methods are called.
Why might you use prepend instead of reopening a class to override a method?
ABecause <code>prepend</code> is slower.
BTo avoid changing the original class directly and keep code modular.
CBecause <code>prepend</code> disables method overriding.
DTo remove methods from the class.
What will this code output?<br>
module M
  def hello
    "Hi from M" + super
  end
end

class C
  def hello
    "Hi from C"
  end
end

C.prepend(M)
puts C.new.hello
AHi from MHi from C
BError
CHi from M
DHi from C
Explain how prepend changes the method lookup chain in Ruby and why that is useful for method chain insertion.
Think about where the module sits compared to the class in the search for methods.
You got /4 concepts.
    Describe how you can use super inside a prepended module's method to extend behavior rather than replace it.
    How do you keep the original method's effect while adding new behavior?
    You got /4 concepts.