Complete the code to define a method that runs when a subclass is created.
class Parent def self.[1](subclass) puts "A new subclass: #{subclass}" end end
The inherited method is a special hook in Ruby that is called when a subclass is created.
Complete the code to call the inherited hook when a subclass is defined.
class Parent def self.inherited(subclass) puts "Subclass created: [1]" end end class Child < Parent end
self which refers to the parent class.The subclass parameter in the inherited method refers to the new subclass being created.
Fix the error in the inherited hook to properly call the parent method.
class Parent def self.inherited(subclass) [1] puts "Subclass: #{subclass}" end end
superclass which is not a method call.Calling super(subclass) ensures the parent class's inherited method is called with the subclass argument.
Fill both blanks to create a subclass that triggers the inherited hook and prints its name.
class Parent def self.inherited(subclass) puts "New subclass: [1]" end end class [2] < Parent end
The subclass parameter holds the subclass name, and Child is the subclass defined.
Fill all three blanks to extend the inherited hook to track subclasses in a list.
class Parent @subclasses = [] def self.inherited(subclass) [1] << [2] puts "Tracking subclass: #{subclass}" end def self.[3] @subclasses end end
The class instance variable @subclasses stores subclasses. The method subclasses returns the list.