Extend for class methods in Ruby - Time & Space Complexity
We want to understand how adding class methods using extend affects the time it takes to run code.
How does the program's work grow when we add methods this way?
Analyze the time complexity of the following code snippet.
module Greetings
def hello
"Hello!"
end
end
class Person
extend Greetings
end
Person.hello
This code adds a class method hello to Person by extending it with the Greetings module.
Look for loops or repeated steps in the code.
- Primary operation: Adding methods to a class using
extendhappens once. - How many times: The extension runs only once when the class is defined or extended.
Adding methods with extend does not depend on input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Constant steps to add methods |
| 100 | Same constant steps |
| 1000 | Still constant steps |
Pattern observation: The work stays the same no matter how many times you call the method later.
Time Complexity: O(1)
This means adding class methods with extend takes the same small amount of time regardless of input size.
[X] Wrong: "Extending a class with many methods makes the program slower every time I call those methods."
[OK] Correct: The extension happens once when setting up the class, not each time you call the methods. Calling the methods later runs only their own code.
Knowing how extend works helps you explain how Ruby adds class methods efficiently. This shows you understand how Ruby organizes code behind the scenes.
"What if we used include instead of extend? How would the time complexity change when adding methods?"
Practice
extend keyword do in Ruby when used inside a class?Solution
Step 1: Understand
Theextendusageextendkeyword adds methods from a module as class methods to the class that calls it.Step 2: Differentiate from
Unlikeincludeinclude, which adds methods as instance methods,extendadds them as class methods.Final Answer:
Adds the module's methods as class methods to the class -> Option CQuick Check:
extend= class methods [OK]
- Confusing extend with include
- Thinking extend adds instance methods
- Assuming extend removes methods
extend?Solution
Step 1: Identify correct
The correct syntax is to callextendusageextendinside the class body with the module name.Step 2: Check each option
class MyClass; extend MyModule; end usesextend MyModuleinside the class, which is correct syntax.Final Answer:
class MyClass; extend MyModule; end -> Option DQuick Check:
Correctextendsyntax = class MyClass; extend MyModule; end [OK]
- Using include instead of extend
- Calling extend on module instead of class
- Incorrect method definition syntax
module Greetings
def hello
"Hello from module"
end
end
class Person
extend Greetings
end
puts Person.helloSolution
Step 1: Understand
The module's methodextend Greetingseffecthellobecomes a class method ofPerson.Step 2: Check method call
Person.hellocalls the class method, which exists and returns "Hello from module".Final Answer:
Hello from module -> Option BQuick Check:
Class method from extend = "Hello from module" [OK]
- Thinking hello is an instance method
- Calling hello on an instance instead of class
- Expecting NoMethodError
module Tools
def tool_name
"Hammer"
end
end
class Worker
extend Tools
def self.tool_name
"Wrench"
end
end
puts Worker.tool_nameSolution
Step 1: Analyze method definitions
The module Tools definestool_name, which is added as a class method to Worker byextend Tools.Step 2: Check method override
Worker defines its own class methodtool_name, which overrides the module method.Final Answer:
No error; output is 'Wrench' -> Option AQuick Check:
Class method override works; output = 'Wrench' [OK]
- Assuming module method overrides class method
- Expecting NoMethodError
- Confusing instance and class methods
info as a class method across multiple classes without repeating code. Which approach using extend is best?
module InfoModule
def info
"Shared class info"
end
end
class Alpha
# What to do here?
end
class Beta
# What to do here?
endSolution
Step 1: Understand sharing class methods
Usingextend InfoModuleinside each class addsinfoas a class method.Step 2: Evaluate options
Addextend InfoModuleinside both Alpha and Beta classes correctly usesextendin both classes to share the method without repetition.Final Answer:
Add extend InfoModule inside both Alpha and Beta classes -> Option AQuick Check:
Use extend in each class to share class methods [OK]
- Using include instead of extend for class methods
- Defining methods separately instead of sharing
- Calling extend on module instead of class
Ready to prove it?
Pass the test and this pattern moves unknown
