Bird
0
0

Why does Ruby use prepend instead of just include to insert modules before a class in the method lookup chain?

hard📝 Conceptual Q10 of 15
Ruby - Modules and Mixins
Why does Ruby use prepend instead of just include to insert modules before a class in the method lookup chain?
ABecause include cannot add instance methods to a class.
BBecause prepend is faster than include.
CBecause prepend allows method overriding without modifying the original class.
DBecause include only works with singleton classes.
Step-by-Step Solution
Solution:
  1. Step 1: Understand difference between prepend and include

    Include adds module after class in lookup, prepend adds before, allowing override without changing class code.
  2. Step 2: Recognize design purpose

    Prepend is designed to let modules override methods cleanly, enabling flexible method chaining and extension.
  3. Final Answer:

    Because prepend allows method overriding without modifying the original class. -> Option C
  4. Quick Check:

    Prepend enables override without class changes [OK]
Quick Trick: Prepend overrides methods without changing original class [OK]
Common Mistakes:
  • Thinking include can't add instance methods
  • Assuming prepend is about speed
  • Confusing include with singleton class usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes