Bird
0
0

Find the error in this Ruby code snippet:

medium📝 Debug Q7 of 15
Ruby - Modules and Mixins
Find the error in this Ruby code snippet:
module Sample
  def self.greet
    puts 'Hello'
  end
end

include Sample
greet
AError: undefined method 'greet' for main:Object
BNo error, prints 'Hello'
CSyntax error in module definition
DError: include cannot be used with modules
Step-by-Step Solution
Solution:
  1. Step 1: Understand method type and include behavior

    The method greet is a module method (defined with self.greet), not an instance method.
  2. Step 2: Analyze the effect of include

    Including the module only mixes in instance methods, so greet is not available as a standalone method after include.
  3. Final Answer:

    Error: undefined method 'greet' for main:Object -> Option A
  4. Quick Check:

    Module methods not mixed in by include = B [OK]
Quick Trick: Include mixes instance, not module methods [OK]
Common Mistakes:
  • Expecting module methods to be available after include
  • Confusing instance and module methods
  • Assuming include imports all methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes