Bird
0
0

Find the problem in this code snippet:

medium📝 Debug Q7 of 15
Ruby - Advanced Metaprogramming

Find the problem in this code snippet:

module M
  module_eval do
    def greet
      'Hello'
    end
  end
end

M.greet
ARuntimeError due to missing class context
BSyntaxError due to wrong module_eval usage
CReturns 'Hello' correctly
DNoMethodError because greet is an instance method, not module method
Step-by-Step Solution
Solution:
  1. Step 1: Identify method type defined by module_eval

    The method greet is defined as an instance method inside the module.

  2. Step 2: Check method call on module

    Calling M.greet tries to call a module method, but greet is not a module method, causing NoMethodError.

  3. Final Answer:

    NoMethodError because greet is an instance method, not module method -> Option D
  4. Quick Check:

    Instance method called on module = NoMethodError [OK]
Quick Trick: Instance methods need an instance, module methods called on module [OK]
Common Mistakes:
  • Calling instance methods on module directly
  • Confusing syntax errors
  • Expecting string output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes