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 Outer
  module Inner
    def self.greet
      "Hello from Inner"
    end
  end
end

puts Outer::Inner.greet
AOuter::Inner
BHello from Inner
CNoMethodError
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand module nesting and method call

    The method greet is defined as a class method on the nested module Inner.
  2. Step 2: Access the method using the namespace operator

    Calling Outer::Inner.greet correctly accesses the method and returns the string.
  3. Final Answer:

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

    Nested module method call = "Hello from Inner" [OK]
Quick Trick: Use :: to call nested module methods [OK]
Common Mistakes:
  • Trying to call method without self prefix
  • Confusing module names with method names
  • Expecting errors due to misunderstanding nesting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes