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 self.say_hello
    "Hello!"
  end
end

puts Greetings.say_hello
AHello!
BGreetings.say_hello
CNoMethodError
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand module method definition

    The method say_hello is defined as a module method using self., so it can be called on the module directly.
  2. Step 2: Evaluate the method call output

    Calling Greetings.say_hello returns the string "Hello!", which is printed by puts.
  3. Final Answer:

    Hello! -> Option A
  4. Quick Check:

    Module method call returns string output [OK]
Quick Trick: Module methods use self. and can be called directly [OK]
Common Mistakes:
  • Trying to call method without self. prefix
  • Expecting instance method behavior
  • Confusing syntax errors with method calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes