Bird
0
0

Identify the error in this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Modules and Mixins
Identify the error in this Ruby code snippet:
module Tools
  def tool_name
    "Hammer"
  end
end

class Worker
  extend Tools

  def self.tool_name
    "Wrench"
  end
end

puts Worker.tool_name
ANo error; output is 'Wrench'
BNoMethodError because tool_name is undefined
CSyntaxError due to extend usage
DOutput is 'Hammer' ignoring class method
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method definitions

    The module Tools defines tool_name, which is added as a class method to Worker by extend Tools.
  2. Step 2: Check method override

    Worker defines its own class method tool_name, which overrides the module method.
  3. Final Answer:

    No error; output is 'Wrench' -> Option A
  4. Quick Check:

    Class method override works; output = 'Wrench' [OK]
Quick Trick: Class methods override extended module methods if defined later [OK]
Common Mistakes:
  • Assuming module method overrides class method
  • Expecting NoMethodError
  • Confusing instance and class methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes