Include for instance methods in Ruby - Time & Space Complexity
When we use include to add instance methods from a module in Ruby, it affects how methods are found and run.
We want to know how the time to find and run these methods changes as the program grows.
Analyze the time complexity of method calls when using include for instance methods.
module Greetings
def hello
"Hello!"
end
end
class Person
include Greetings
def greet
hello
end
end
person = Person.new
person.greet
This code adds the hello method from the Greetings module to instances of Person using include.
Look at what happens when greet calls hello:
- Primary operation: Ruby looks up the
hellomethod in the method lookup path. - How many times: Each time
hellois called, Ruby searches the class and included modules.
As you add more modules or methods, Ruby checks each place in order to find the method.
| Input Size (number of modules/methods) | Approx. Operations |
|---|---|
| 1 | 1 check |
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
Pattern observation: The more modules included, the longer it takes to find the method, growing roughly in a straight line.
Time Complexity: O(n)
This means the time to find an instance method grows linearly with the number of modules and classes Ruby must check.
[X] Wrong: "Including a module makes method calls instant no matter how many modules there are."
[OK] Correct: Ruby must search through each included module in order, so more modules mean more work to find the method.
Understanding how Ruby finds methods when using include helps you explain performance in real Ruby programs and shows you know how method lookup works behind the scenes.
"What if we used extend instead of include? How would the time complexity of method lookup change?"
Practice
include keyword do in Ruby when used inside a class?Solution
Step 1: Understand the role of
Theincludeincludekeyword is used to add methods from a module into a class.Step 2: Identify how methods are added
When a module is included, its methods become instance methods of the class, meaning objects of the class can call them.Final Answer:
It adds the module's methods as instance methods to the class. -> Option DQuick Check:
Include = instance methods added [OK]
- Confusing include with extend (which adds class methods)
- Thinking include creates a new module
- Assuming include removes existing methods
Greetings inside a class Person?Solution
Step 1: Recall correct syntax for including modules
In Ruby, to add instance methods from a module, useinclude ModuleNameinside the class body without parentheses.Step 2: Check each option
class Person; include Greetings; end usesinclude Greetingscorrectly. class Person; extend Greetings; end usesextendwhich adds class methods, not instance methods. class Person; include(Greetings); end uses parentheses which is not standard syntax. class Person; use Greetings; end usesusewhich is invalid in Ruby.Final Answer:
class Person; include Greetings; end -> Option AQuick Check:
Include syntax = include ModuleName [OK]
- Using extend instead of include
- Adding parentheses after include
- Using invalid keywords like 'use'
module Talk
def speak
"Hello!"
end
end
class Dog
include Talk
end
puts Dog.new.speakSolution
Step 1: Understand module inclusion
The moduleTalkdefines an instance methodspeak. Including it inDogaddsspeakas an instance method ofDog.Step 2: Trace the method call
Creating a newDogobject and callingspeakwill execute the method fromTalk, returning "Hello!".Final Answer:
Hello! -> Option BQuick Check:
Included method called = "Hello!" [OK]
- Expecting NoMethodError because of missing method
- Confusing instance and class methods
- Thinking output is method name or nil
module Walk
def walk
"Walking"
end
end
class Person
extend Walk
end
puts Person.new.walkSolution
Step 1: Understand extend vs include
Usingextend Walkadds module methods as class methods, not instance methods.Step 2: Analyze method call
Person.new.walkcallswalkon an instance, butwalkis only a class method here, so it raises NoMethodError.Final Answer:
NoMethodError because walk is not an instance method -> Option AQuick Check:
Extend adds class methods, instance call fails [OK]
- Confusing extend with include
- Expecting no error on instance method call
- Thinking extend causes syntax or type errors
puts Cat.new.sound output?module AnimalSounds
def sound
"Generic sound"
end
end
class Dog
include AnimalSounds
def sound
"Bark"
end
end
class Cat
include AnimalSounds
endSolution
Step 1: Check method definitions in classes
ClassDogoverridessoundmethod with "Bark". ClassCatdoes not override it.Step 2: Determine method lookup for Cat instance
SinceCatincludesAnimalSoundsand does not overridesound, callingsoundonCat.newuses the module's method returning "Generic sound".Final Answer:
Generic sound -> Option CQuick Check:
Included method used if no override [OK]
- Assuming Cat uses Dog's sound method
- Expecting NoMethodError for missing method
- Confusing instance and class methods
Ready to prove it?
Pass the test and this pattern moves unknown
