Bird
0
0

Identify the error in the following Ruby code:

medium📝 Debug Q14 of 15
Ruby - Modules and Mixins
Identify the error in the following Ruby code:
module Walk
  def walk
    "Walking"
  end
end

class Person
  extend Walk
end

puts Person.new.walk
ANoMethodError because walk is not an instance method
BSyntaxError due to extend usage
CNo error, outputs "Walking"
DTypeError because module cannot be extended
Step-by-Step Solution
Solution:
  1. Step 1: Understand extend vs include

    Using extend Walk adds module methods as class methods, not instance methods.
  2. Step 2: Analyze method call

    Person.new.walk calls walk on an instance, but walk is only a class method here, so it raises NoMethodError.
  3. Final Answer:

    NoMethodError because walk is not an instance method -> Option A
  4. Quick Check:

    Extend adds class methods, instance call fails [OK]
Quick Trick: Extend adds class methods; instance calls fail [OK]
Common Mistakes:
  • Confusing extend with include
  • Expecting no error on instance method call
  • Thinking extend causes syntax or type errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes