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 hello
    "Hello"
  end
end

class A
  include M
  extend M
end

puts A.hello
puts A.new.hello
ANoMethodError on A.new.hello
BNoMethodError on A.hello
CNo error, code runs fine
DSyntaxError due to extend and include
Step-by-Step Solution
Solution:
  1. Step 1: Understand include and extend

    include M adds hello as instance method; extend M adds hello as class method.
  2. Step 2: Check method calls

    A.new.hello works because of include. A.hello calls class method added by extend.
  3. Step 3: Identify error

    Both calls are valid, so no error occurs.
  4. Final Answer:

    No error, code runs fine -> Option C
  5. Quick Check:

    include adds instance, extend adds class methods [OK]
Quick Trick: include for instance, extend for class methods can coexist [OK]
Common Mistakes:
  • Assuming extend overrides include
  • Expecting NoMethodError on instance method
  • Thinking extend causes syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes