Bird
0
0

Which of the following is the correct way to define an inherited hook in a Ruby class?

easy📝 Syntax Q12 of 15
Ruby - Advanced Metaprogramming
Which of the following is the correct way to define an inherited hook in a Ruby class?
Adef self.inherited; puts "New subclass created"; end
Bdef inherited(self); puts "New subclass created"; end
Cdef inherited(subclass); puts "New subclass created"; end
Ddef self.inherited(subclass); puts "New subclass created"; end
Step-by-Step Solution
Solution:
  1. Step 1: Recognize the method signature

    The inherited hook must be defined as a class method, so it needs self.inherited and takes one argument: the subclass.
  2. Step 2: Check method parameters and syntax

    def self.inherited(subclass); puts "New subclass created"; end correctly defines the class method with the subclass parameter. Incorrect versions lack self. (instance method), omit the subclass parameter, or use wrong parameters like self.
  3. Final Answer:

    def self.inherited(subclass); puts "New subclass created"; end -> Option D
  4. Quick Check:

    Class method with subclass param = def self.inherited(subclass); puts "New subclass created"; end [OK]
Quick Trick: Inherited hook is a class method with subclass param [OK]
Common Mistakes:
  • Defining inherited as instance method
  • Omitting the subclass parameter
  • Forgetting self. for class method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes