Bird
0
0

Identify the error in this Ruby code using the included hook:

medium📝 Debug Q6 of 15
Ruby - Advanced Metaprogramming

Identify the error in this Ruby code using the included hook:

module M
  def included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def hello
      "Hi"
    end
  end
end

class C
  include M
end

puts C.hello
Aextend should be include
BClassMethods module is missing
Cincluded should be a class method (self.included)
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check included hook definition

    The included hook must be defined as a class method with self.included, not as an instance method.
  2. Step 2: Understand the impact

    Defining it without self. means the hook never runs, so hello is not added as a class method.
  3. Final Answer:

    included should be a class method (self.included) -> Option C
  4. Quick Check:

    included hook must be self.included [OK]
Quick Trick: Define included as self.included to trigger hook [OK]
Common Mistakes:
  • Defining included as instance method
  • Forgetting self. prefix
  • Confusing extend with include

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes