Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Modules and Mixins
What will this Ruby code print?
module A
  def hello
    "Hello from A"
  end
end

class B
  include A
  def hello
    "Hello from B"
  end
end

puts B.new.hello
ANoMethodError
BHello from A
CHello from B
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Understand method lookup with include

    Class B includes module A but also defines its own hello method, which overrides the module's method.
  2. Step 2: Calling hello on B instance

    B.new.hello calls B's own method, returning "Hello from B".
  3. Final Answer:

    Hello from B -> Option C
  4. Quick Check:

    Class method overrides included module method [OK]
Quick Trick: Class methods override included module methods [OK]
Common Mistakes:
  • Expecting module method to run instead
  • Confusing method override order
  • Thinking NoMethodError occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes