Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Modules and Mixins
Identify the error in this Ruby code:
module M
  def greet
    "Hello"
  end
end

class C
  include M
  def greet
    "Hi"
  end
end

puts C.greet
ACalling greet on class instead of instance causes error
BModule M is not included properly
CMethod greet is missing in module
DSyntax error in class definition
Step-by-Step Solution
Solution:
  1. Step 1: Check method call

    The code calls C.greet, but greet is an instance method, not a class method.
  2. Step 2: Understand instance vs class methods

    Since greet is defined as instance method, calling it on the class causes a NoMethodError.
  3. Final Answer:

    Calling greet on class instead of instance causes error -> Option A
  4. Quick Check:

    Instance method called on class = error [OK]
Quick Trick: Call instance methods on objects, not classes [OK]
Common Mistakes:
  • Calling instance method on class
  • Thinking include is incorrect
  • Assuming syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes