Inherited hook in Ruby - Time & Space Complexity
When Ruby classes inherit from each other, the inherited hook runs. We want to see how the time it takes to run this hook changes as more classes inherit.
How does the number of inherited classes affect the total work done by the hook?
Analyze the time complexity of the following Ruby code using the inherited hook.
class Parent
def self.inherited(subclass)
puts "New subclass: #{subclass}"
end
end
class Child1 < Parent; end
class Child2 < Parent; end
class Child3 < Parent; end
This code prints a message every time a new subclass inherits from Parent.
Look at what repeats when new subclasses are created.
- Primary operation: The
inheritedmethod runs once for each subclass. - How many times: Once per subclass created, so if there are
nsubclasses, it runsntimes.
As the number of subclasses grows, the total times the hook runs grows the same way.
| Input Size (n subclasses) | Approx. Operations (hook calls) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly with the number of subclasses.
Time Complexity: O(n)
This means the total work done by the inherited hook grows in a straight line with the number of subclasses.
[X] Wrong: "The inherited hook runs only once no matter how many subclasses there are."
[OK] Correct: The hook runs every time a new subclass is created, so it runs as many times as subclasses exist.
Understanding how hooks like inherited scale helps you reason about code that runs automatically during class creation. This skill shows you can think about hidden costs in programs.
What if the inherited hook also created a new subclass inside itself? How would that affect the time complexity?
Practice
inherited hook method do in Ruby?Solution
Step 1: Understand the purpose of
Theinheritedinheritedmethod 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 AQuick Check:
Inherited hook = called on subclass creation [OK]
- Confusing inherited with initialize method
- Thinking it runs on instance creation
- Mixing it with method override hooks
inherited hook in a Ruby class?Solution
Step 1: Recognize the method signature
Theinheritedhook must be defined as a class method, so it needsself.inheritedand takes one argument: the subclass.Step 2: Check method parameters and syntax
def self.inherited(subclass); puts "New subclass created"; endcorrectly defines the class method with the subclass parameter. Incorrect versions lackself.(instance method), omit thesubclassparameter, or use wrong parameters likeself.Final Answer:
def self.inherited(subclass); puts "New subclass created"; end -> Option DQuick Check:
Class method with subclass param = def self.inherited(subclass); puts "New subclass created"; end [OK]
- Defining inherited as instance method
- Omitting the subclass parameter
- Forgetting self. for class method
class Parent
def self.inherited(subclass)
puts "Subclass: #{subclass}"
end
end
class Child < Parent
endSolution
Step 1: Understand when inherited runs
Theinheritedmethod inParentruns automatically when a subclass is created.Step 2: Identify subclass creation
WhenChild < Parentis declared,Parent.inherited(Child)is called, printing "Subclass: Child".Final Answer:
Subclass: Child -> Option BQuick Check:
Subclass creation triggers inherited hook output [OK]
- Expecting output on instance creation
- Confusing subclass with parent class name
- Thinking no output occurs
inherited hook:class Base
def inherited(subclass)
puts "Subclass created: #{subclass}"
end
end
class Derived < Base
endSolution
Step 1: Check method definition context
Theinheritedhook must be a class method, so it requiresdef self.inherited(subclass).Step 2: Identify the error in the code
Here,inheritedis defined as an instance method, missingself., so it won't be called on subclass creation.Final Answer:
The inherited method should be a class method with self. -> Option AQuick Check:
Inherited hook must be class method [OK]
- Defining inherited as instance method
- Omitting self. in method definition
- Misunderstanding inherited hook usage
inherited hook to automatically add a class variable @@subclasses that tracks all subclasses of a parent class?Solution
Step 1: Understand subclass tracking
To track subclasses, use a class variable@@subclassesinitialized as an array in the parent class.Step 2: Use inherited hook to update tracking
Defineself.inherited(subclass)to append the new subclass to@@subclassesautomatically when a subclass is created.Final Answer:
Defineself.inherited(subclass)to append subclass to@@subclassesarray. -> Option CQuick Check:
Inherited hook + class variable tracks subclasses [OK]
- Using instance methods to track subclasses
- Tracking subclasses in initialize method
- Forgetting to initialize @@subclasses as array
Ready to prove it?
Pass the test and this pattern moves unknown
