📖 Scenario: Imagine you are creating a system to track different types of vehicles. You want to know whenever a new type of vehicle class is created that inherits from your main Vehicle class.
🎯 Goal: You will build a Ruby program that uses the inherited hook method to print a message every time a new subclass inherits from Vehicle.
📋 What You'll Learn
Create a class called Vehicle
Define an inherited method inside Vehicle that takes one argument called subclass
Inside the inherited method, print a message using puts that says: "New vehicle type created: "
Create two subclasses called Car and Truck that inherit from Vehicle
Print the messages when Car and Truck are defined
💡 Why This Matters
🌍 Real World
Using the <code>inherited</code> hook helps frameworks and libraries track subclasses automatically, such as in web frameworks or plugin systems.
💼 Career
Understanding hooks like <code>inherited</code> is useful for Ruby developers building extensible applications or working with metaprogramming.
Progress0 / 4 steps
1
Create the Vehicle class
Create a class called Vehicle with no content inside it.
Ruby
Hint
Use class Vehicle and end to define the class.
2
Add the inherited hook method
Inside the Vehicle class, define a method called self.inherited that takes one parameter called subclass. Inside this method, use puts to print "New vehicle type created: " followed by the subclass name using subclass.name.
Ruby
Hint
The inherited method is a class method, so use self.inherited.
3
Create subclasses Car and Truck
Create two subclasses called Car and Truck that inherit from Vehicle. Use the syntax class Car < Vehicle and class Truck < Vehicle.
Ruby
Hint
Use class Car < Vehicle and class Truck < Vehicle to create subclasses.
4
Run the program to see output
Run the program and observe the output. It should print two lines: "New vehicle type created: Car" and "New vehicle type created: Truck".
Ruby
Hint
Just run the program. The messages print automatically when subclasses are defined.
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".