Bird
0
0

Given this code:

hard📝 Application Q9 of 15
Ruby - Modules and Mixins
Given this code:
module A
  def call
    "A" + super
  end
end

module B
  def call
    "B" + super
  end
end

class X
  prepend A
  prepend B
  def call
    "X"
  end
end

puts X.new.call

What is the output?
ABAX
BABX
CXBA
DError due to multiple prepends
Step-by-Step Solution
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

    Calling call runs B's method, which calls super (A), which calls super (X), returning "X".
  3. Step 3: Concatenate results

    B adds "B", A adds "A", X returns "X", combined "BAX".
  4. Final Answer:

    BAX -> Option A
  5. Quick Check:

    Last prepended module runs first, output = BAX [OK]
Quick Trick: Last prepended module runs first in method chain [OK]
Common Mistakes:
  • Assuming first prepended runs first
  • Expecting ABX instead of BAX
  • Thinking multiple prepends cause error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes