Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Modules and Mixins
What will be the output of this Ruby code?
module M
  def greet
    "Hi"
  end
end

class C
  extend M
end

puts C.greet
Anil
BNoMethodError
CHi
DError: undefined method greet
Step-by-Step Solution
Solution:
  1. Step 1: Understand extend effect

    Extending module M adds its methods as class methods to class C.
  2. Step 2: Call class method greet

    Calling C.greet invokes M#greet, returning "Hi".
  3. Final Answer:

    Hi -> Option C
  4. Quick Check:

    extend adds class method = "Hi" [OK]
Quick Trick: extend adds module methods as class methods callable on class [OK]
Common Mistakes:
  • Expecting greet as instance method
  • Confusing NoMethodError with correct call
  • Thinking extend adds instance methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes