Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Modules and Mixins
What will be the output of this Ruby code?
module Greetings
  def self.say_hello(name)
    "Hello, #{name}!"
  end
end

puts Greetings.say_hello('Alice')
AError: undefined method say_hello
BHello, #{name}!
CHello, Alice!
DGreetings.say_hello
Step-by-Step Solution
Solution:
  1. Step 1: Identify method call and string interpolation

    The method say_hello is a module method called with Greetings.say_hello('Alice'). It returns a string with #{name} interpolated.
  2. Step 2: Evaluate the output

    The interpolation replaces #{name} with "Alice", so the output is "Hello, Alice!".
  3. Final Answer:

    Hello, Alice! -> Option C
  4. Quick Check:

    String interpolation works in module methods = C [OK]
Quick Trick: String interpolation works normally in module methods [OK]
Common Mistakes:
  • Expecting literal string with #{name}
  • Thinking method is undefined without include
  • Confusing instance and module method calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes