The <code>inherited</code> hook is a special method in Ruby that is called automatically whenever a subclass is created from a class. It lets the parent class react to the creation of its child classes.
Click to reveal answer
beginner
How do you define an inherited hook in a Ruby class?
You define a class method named <code>self.inherited(subclass)</code> inside the parent class. Ruby calls this method with the new subclass as the argument whenever a subclass is created.
Click to reveal answer
intermediate
What can you do inside the inherited hook?
Inside the inherited hook, you can run code to customize or track subclasses. For example, you can add methods, set variables, or keep a list of all subclasses.
Click to reveal answer
beginner
Example: What will this code print?<br><pre>class Parent
def self.inherited(subclass)
puts "New subclass: #{subclass}"
end
end
class Child < Parent
end</pre>
It will print: New subclass: Child This happens because when Child inherits from Parent, the inherited hook runs and prints the message.
Click to reveal answer
intermediate
Why is the inherited hook useful in Ruby?
It helps you automatically run code when new subclasses are made. This is useful for tracking subclasses, adding shared behavior, or setting up configurations without changing each subclass manually.
Click to reveal answer
What argument does the inherited hook receive?
AThe parent class
BThe new subclass
CAn instance of the subclass
DNo arguments
✗ Incorrect
The inherited hook receives the new subclass as its argument.
Where do you define the inherited method?
AInside an instance method
BInside the subclass
COutside any class
DInside the parent class as a class method
✗ Incorrect
You define self.inherited(subclass) as a class method inside the parent class.
What happens when a subclass is created and the parent class has an inherited hook?
The inherited method runs automatically when a subclass is created.
Which of these is a common use of the inherited hook?
ATo track all subclasses
BTo create instances
CTo override instance methods
DTo delete subclasses
✗ Incorrect
Tracking all subclasses is a common use of the inherited hook.
Can the inherited hook modify the subclass?
AYes, but only instance methods
BNo, it only reads subclass info
CYes, it can add methods or variables
DNo, it only runs after subclass is deleted
✗ Incorrect
The inherited hook can modify the subclass by adding methods or variables.
Explain what the inherited hook does in Ruby and give a simple example.
Think about what happens when you create a new subclass.
You got /3 concepts.
Describe a practical use case for the inherited hook in a Ruby program.
Why would you want to know when a subclass is created?
You got /3 concepts.
Practice
(1/5)
1. What does the inherited hook method do in Ruby?
easy
A. It is called automatically when a subclass is created.
B. It runs when an instance of a class is created.
C. It is used to define class variables.
D. It is called when a method is overridden.
Solution
Step 1: Understand the purpose of inherited
The inherited method is a special hook in Ruby that triggers when a subclass is created from a parent class.
Step 2: Differentiate from other hooks
It is not related to instance creation, method overriding, or variable definition, but specifically to subclass creation.
Final Answer:
It is called automatically when a subclass is created. -> Option A
Quick Check:
Inherited hook = called on subclass creation [OK]
Hint: Inherited hook triggers only on subclass creation [OK]
Common Mistakes:
Confusing inherited with initialize method
Thinking it runs on instance creation
Mixing it with method override hooks
2. Which of the following is the correct way to define an inherited hook in a Ruby class?
easy
A. def self.inherited; puts "New subclass created"; end
B. def inherited(self); puts "New subclass created"; end
C. def inherited(subclass); puts "New subclass created"; end
D. def self.inherited(subclass); puts "New subclass created"; end
Solution
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.
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.
Final Answer:
def self.inherited(subclass); puts "New subclass created"; end -> Option D
Quick Check:
Class method with subclass param = def self.inherited(subclass); puts "New subclass created"; end [OK]
Hint: 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
3. What will be the output of this Ruby code?
class Parent
def self.inherited(subclass)
puts "Subclass: #{subclass}"
end
end
class Child < Parent
end
medium
A. No output
B. Subclass: Child
C. Subclass: Parent
D. SyntaxError
Solution
Step 1: Understand when inherited runs
The inherited method in Parent runs automatically when a subclass is created.
Step 2: Identify subclass creation
When Child < Parent is declared, Parent.inherited(Child) is called, printing "Subclass: Child".