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 Alpha
  module Beta
    def self.message
      'Greetings from Beta'
    end
  end
end

puts Alpha::Beta.message
AGreetings from Beta
BAlpha::Beta.message
CError: undefined method 'message'
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Identify module nesting

    Module Beta is nested inside module Alpha.
  2. Step 2: Understand method call

    message is a class method (self.message) inside Beta, so it can be called as Alpha::Beta.message.
  3. Step 3: Output

    Calling puts Alpha::Beta.message prints the returned string 'Greetings from Beta'.
  4. Final Answer:

    Greetings from Beta -> Option A
  5. Quick Check:

    Nested module method called with :: [OK]
Quick Trick: Call nested module methods with :: and self. [OK]
Common Mistakes:
  • Trying to call instance methods without an object
  • Using dot notation instead of :: for modules
  • Forgetting self. for class methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes