Module methods in Ruby - Time & Space Complexity
We want to understand how long it takes to run code that uses module methods in Ruby.
Specifically, we ask: how does the time to run change as the input grows?
Analyze the time complexity of the following code snippet.
module MathHelpers
def self.square_all(numbers)
numbers.map { |n| n * n }
end
end
n = 10
arr = (1..n).to_a
result = MathHelpers.square_all(arr)
This code defines a module method that squares each number in an array.
- Primary operation: The method uses
mapto go through each number once. - How many times: It runs once for each number in the input array.
As the input array gets bigger, the method does more work, one step per number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly with the number of items; doubling items doubles work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the input size.
[X] Wrong: "Module methods make the code run instantly, no matter the input size."
[OK] Correct: Module methods are just like regular methods; if they process each item, time grows with input size.
Understanding how module methods handle input size helps you explain code efficiency clearly and confidently.
What if the method used nested loops inside the module method? How would the time complexity change?
Practice
Solution
Step 1: Understand module method definition
In Ruby, to make a method callable directly on a module, you define it withself.method_nameinside the module.Step 2: Compare options
Define the method withself.method_nameinside the module usesself.method_name, which is correct. Other options either omitselfor use invalid syntax.Final Answer:
Define the method with self.method_name inside the module -> Option CQuick Check:
Module methods use self.method_name = A [OK]
- Forgetting to add self before method name
- Defining methods outside the module
- Using invalid syntax like def module.method_name
greet defined in module Welcome?Solution
Step 1: Recall how to call module methods
Module methods are called using dot notation:ModuleName.method_name.Step 2: Check options
Welcome.greetis the correct syntax using dot notation.Welcome#greetincorrectly uses #.greet.Welcomereverses the order.Welcome.new.greetconfuses it with an instance method call.Final Answer:
Welcome.greet -> Option DQuick Check:
Call module methods with dot = D [OK]
- Using # instead of . to call module methods
- Trying to call method with invalid syntax
- Confusing instance and module method calls
module MathTools
def self.square(x)
x * x
end
end
puts MathTools.square(4)Solution
Step 1: Understand the method definition
The methodsquareis defined as a module method usingself.square. It returns the square of the input.Step 2: Calculate the output
CallingMathTools.square(4)returns 4 * 4 = 16, which is printed.Final Answer:
16 -> Option AQuick Check:
4 squared = 16 [OK]
- Forgetting self and causing undefined method error
- Confusing output with input
- Expecting method name as output
module Greetings
def greet
"Hello!"
end
end
puts Greetings.greetSolution
Step 1: Check method definition inside module
The methodgreetis defined withoutself., so it is an instance method, not a module method.Step 2: Understand method call
CallingGreetings.greettries to call a module method, but none exists, causing an error.Final Answer:
Method greet is not defined as a module method -> Option AQuick Check:
Missing self. means no module method [OK]
- Defining instance methods instead of module methods
- Calling module method without defining it
- Assuming parentheses are required in method call
double that returns twice the input number?module Calculator # Add your method here end
Solution
Step 1: Define a module method with self
To create a module method, define it withself.method_nameinside the module.Step 2: Write method to return twice the input
The method should take one argumentxand returnx * 2.Final Answer:
def self.double(x) x * 2 end -> Option BQuick Check:
Module method with self and correct logic = A [OK]
- Omitting self and creating instance method
- Missing method argument
- Defining method outside module
Ready to prove it?
Pass the test and this pattern moves unknown
