Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Advanced Metaprogramming
Identify the error in this Ruby code:
class Test
  def method_added(name)
    puts "Added: #{name}"
  end

  def example; end
end
ANo error, code runs fine
Bmethod_added must not accept any parameters
Cexample method must be defined after method_added
Dmethod_added should be a class method, not instance method
Step-by-Step Solution
Solution:
  1. Step 1: Check method_added definition

    It is defined as an instance method, but Ruby expects it as a class method to hook method additions.
  2. Step 2: Understand consequence

    Because it's not a class method, the hook won't be called when instance methods are added.
  3. Final Answer:

    method_added should be a class method, not instance method -> Option D
  4. Quick Check:

    method_added must be class method [OK]
Quick Trick: method_added must be defined with self. to work [OK]
Common Mistakes:
  • Defining method_added as instance method
  • Ignoring parameter requirement
  • Assuming order of method definitions matters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes