Bird
0
0

What is the output of this Ruby code?

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

class Person
  include Greetings
end

p = Person.new
puts p.greet
Anil
BError: undefined method greet
CHi!
DGreetings
Step-by-Step Solution
Solution:
  1. Step 1: Understand module inclusion

    The Greetings module defines greet method. Including it in Person adds greet as an instance method.
  2. Step 2: Call the method on instance

    Creating p as Person.new and calling p.greet returns "Hi!".
  3. Final Answer:

    Hi! -> Option C
  4. Quick Check:

    Include module = instance method available [OK]
Quick Trick: Included module methods become instance methods [OK]
Common Mistakes:
  • Expecting error due to missing method
  • Confusing module name with output
  • Assuming nil return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes