Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Modules and Mixins
What will be the output of the following Ruby code?
module Talk
  def speak
    "Hello!"
  end
end

class Dog
  include Talk
end

puts Dog.new.speak
ANoMethodError
BHello!
Cspeak
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Understand module inclusion

    The Talk module defines speak method. Including it in Dog adds speak as an instance method.
  2. Step 2: Call the method on Dog instance

    Dog.new.speak calls the method and returns "Hello!" string.
  3. Final Answer:

    Hello! -> Option B
  4. Quick Check:

    Included method call = "Hello!" [OK]
Quick Trick: Included module methods become instance methods [OK]
Common Mistakes:
  • Expecting NoMethodError because of missing method
  • Confusing include with extend
  • Thinking output is method name or nil

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes