Bird
0
0

What will happen if you run this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Modules and Mixins
What will happen if you run this Ruby code?
module Sample
  def greet
    "Hello"
  end
end

puts Sample.greet
AHello
BSyntaxError
Cnil
DNoMethodError
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method definition

    The method greet is an instance method inside the module, not a module method.
  2. Step 2: Calling method on module itself

    Calling Sample.greet tries to call a module method, but greet is not defined as such, causing NoMethodError.
  3. Final Answer:

    NoMethodError -> Option D
  4. Quick Check:

    Instance methods can't be called on module directly [OK]
Quick Trick: Instance methods need module inclusion; module methods use self. prefix [OK]
Common Mistakes:
  • Calling instance methods on module directly
  • Expecting string output
  • Confusing instance and module methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes