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 Greetings
  def hello
    "Hello from module"
  end
end

class Person
  extend Greetings
end

puts Person.hello
ANoMethodError: undefined method 'hello' for Person
BHello from module
CHello from module called on instance
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand extend Greetings effect

    The module's method hello becomes a class method of Person.
  2. Step 2: Check method call

    Person.hello calls the class method, which exists and returns "Hello from module".
  3. Final Answer:

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

    Class method from extend = "Hello from module" [OK]
Quick Trick: extend adds module methods as class methods, so Person.hello works [OK]
Common Mistakes:
  • Thinking hello is an instance method
  • Calling hello on an instance instead of class
  • Expecting NoMethodError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes