Included hook in Ruby - Time & Space Complexity
We want to understand how the time cost changes when Ruby runs code using the included hook.
Specifically, how does adding this hook affect the work done as the program grows?
Analyze the time complexity of this Ruby module using the included hook.
module Greetings
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def say_hello
puts "Hello!"
end
end
end
class Person
include Greetings
end
Person.say_hello
This code adds class methods to a class when the module is included using the included hook.
Look for loops or repeated actions in the code.
- Primary operation: The
includedhook runs once per class that includes the module. - How many times: Exactly once for each class that uses
include Greetings.
Imagine adding more classes that include the module.
| Number of Classes (n) | Approx. Operations |
|---|---|
| 10 | 10 times the hook runs |
| 100 | 100 times the hook runs |
| 1000 | 1000 times the hook runs |
Each new class that includes the module triggers the hook once, so work grows directly with the number of classes.
Time Complexity: O(n)
This means the time grows linearly with how many classes include the module.
[X] Wrong: "The included hook runs once and then never again no matter how many classes include the module."
[OK] Correct: The hook runs every time a new class includes the module, so it runs once per class, not just once total.
Understanding how hooks like included run helps you explain how Ruby modules add behavior efficiently as programs grow.
What if the included hook added methods by looping over a large list each time? How would that change the time complexity?
Practice
What does the included hook do in a Ruby module?
Solution
Step 1: Understand the purpose of the included hook
The included hook is a special method in Ruby modules that runs automatically when the module is included in a class.Step 2: Compare options with the definition
Only It runs code automatically when the module is included in a class. correctly describes this automatic behavior. Other options describe manual calls, prevention, or extension, which are incorrect.Final Answer:
It runs code automatically when the module is included in a class. -> Option BQuick Check:
Included hook = automatic run on include [OK]
- Thinking included runs on extend instead of include
- Believing included requires manual method calls
- Confusing included with preventing inclusion
Which of the following is the correct syntax to define an included hook inside a Ruby module?
module Example # What goes here? end
Solution
Step 1: Recall how to define class methods in modules
The included hook must be a module method, so it needs to be defined with self.included.Step 2: Check the method signature
The method receives the including class as a parameter, so it must accept one argument (base).Final Answer:
def self.included(base) puts "Included in #{base}" end -> Option AQuick Check:
included hook = def self.included(base) [OK]
- Defining included without self (instance method instead of module method)
- Missing the base parameter
- Using def self.include instead of included
What will be the output of this Ruby code?
module Greetings
def self.included(base)
puts "Greetings included in #{base}"
end
end
class Person
include Greetings
endSolution
Step 1: Identify when included hook runs
The included hook runs automatically when the module is included in a class.Step 2: Check the output inside the included method
The method prints "Greetings included in #{base}", where base is the class Person.Final Answer:
Greetings included in Person -> Option CQuick Check:
included hook prints class name on include [OK]
- Expecting no output because included is a hook
- Confusing base with module name
- Thinking included runs on class definition, not include
Find the error in this Ruby code using the included hook:
module Logger
def included(base)
base.extend(ClassMethods)
end
module ClassMethods
def log(msg)
puts "Log: #{msg}"
end
end
end
class App
include Logger
end
App.log("Start")Solution
Step 1: Check how included hook is defined
The included hook must be a module method, defined as self.included, not an instance method.Step 2: Understand the effect of incorrect definition
Defining included without self means it won't run automatically on include, so base.extend won't happen.Final Answer:
The included method should be defined as self.included, not instance method. -> Option DQuick Check:
included hook must be self.included [OK]
- Defining included without self
- Confusing extend and include in hook
- Trying to call class method on instance
You want to add a class method greet to any class that includes your module Friendly. Which is the best way to do this using the included hook?
module Friendly # Fill in the included hook end class Dog include Friendly end puts Dog.greet
Solution
Step 1: Understand how to add class methods via included hook
To add class methods, define self.included(base) and call base.extend with a module containing those methods.Step 2: Why other options fail
Instance methods won't be class methods; defining class methods directly in module won't add them to including class; calling include inside method is incorrect.Final Answer:
Define self.included(base) and inside it call base.extend with a module containing greet. -> Option AQuick Check:
Use included hook + base.extend for class methods [OK]
- Defining greet as instance method instead of class method
- Not using included hook to extend class
- Trying to call include inside a method
Ready to prove it?
Pass the test and this pattern moves unknown
