Super keyword behavior in Ruby - Time & Space Complexity
We want to understand how using the super keyword affects the time it takes for a program to run.
Specifically, how does calling a method from a parent class impact execution as input grows?
Analyze the time complexity of the following Ruby code using super.
class Parent
def process(items)
items.map { |item| item * 2 }
end
end
class Child < Parent
def process(items)
result = super
result.select { |x| x > 10 }
end
end
child = Child.new
child.process([1, 5, 10, 20])
This code doubles each item in a list using the parent method, then filters the doubled items in the child method.
Look for loops or repeated actions in the code.
- Primary operation: Two loops over the list: one in the parent
processmethod (map), and one in the child method (select). - How many times: Each loop runs once over all items, so twice over the input list.
Each item in the input list is processed twice, once for doubling and once for filtering.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (2 loops x 10 items) |
| 100 | About 200 operations |
| 1000 | About 2000 operations |
Pattern observation: The total work grows roughly twice as fast as the input size.
Time Complexity: O(n)
This means the time to run grows directly in proportion to the number of items.
[X] Wrong: "Using super makes the method twice as slow in a way that changes the overall time complexity."
[OK] Correct: Calling super just adds another loop over the data, so it still grows linearly with input size, not exponentially or worse.
Understanding how method calls like super affect performance helps you explain your code clearly and reason about efficiency in real projects.
What if the parent method used nested loops instead of a single loop? How would the time complexity change?
Practice
super keyword do in Ruby?Solution
Step 1: Understand the role of
Thesupersuperkeyword is used inside a child class method to call the method with the same name from its parent class. It does not create, override, or delete methods but simply calls the parent method to reuse or extend its behavior.Final Answer:
Calls the parent class's method with the same name -> Option CQuick Check:
supercalls parent method [OK]
- Thinking super creates or deletes methods
- Confusing super with instance creation
- Assuming super overrides methods permanently
super?Solution
Step 1: Understand
Usingsuperwith parenthesessuper()calls the parent method explicitly with no arguments, ignoring any current method arguments. Onlysuper()uses parentheses correctly to pass no arguments; other forms are invalid syntax.Final Answer:
super() -> Option AQuick Check:
super()means no arguments [OK]
- Using super without parentheses to mean no arguments
- Using brackets or braces instead of parentheses
- Confusing super() with super without parentheses
class Parent
def greet(name)
"Hello, #{name} from Parent"
end
end
class Child < Parent
def greet(name)
super + " and Child"
end
end
puts Child.new.greet("Alice")Solution
Step 1: Trace the method call in Child#greet
The Child'sgreetmethod callssuperwithout parentheses, so it passes the argument "Alice" to Parent#greet. Parent#greet returns "Hello, Alice from Parent". Child appends " and Child" to this string.Final Answer:
Hello, Alice from Parent and Child -> Option AQuick Check:
superpasses args, output combines strings [OK]
- Assuming super passes no arguments by default
- Expecting an error due to argument mismatch
- Ignoring the appended string in Child method
class Parent
def initialize(age)
@age = age
end
end
class Child < Parent
def initialize
super
@name = "Child"
end
end
Child.newSolution
Step 1: Check Parent initialize method
Parent'sinitializerequires one argumentage. Child'sinitializecallssuperwithout arguments, so it tries to call Parent'sinitializewith zero arguments, causing an ArgumentError.Final Answer:
ArgumentError: wrong number of arguments for super -> Option DQuick Check:
super must match parent args or use super() [OK]
- Assuming super() passes parent's required args automatically
- Ignoring that Parent requires an argument
- Thinking missing parentheses cause syntax error here
Child.new.greet output?
class Parent
def greet(name = "Guest")
"Hello, #{name} from Parent"
end
end
class Child < Parent
def greet(name = nil)
if name
super(name.upcase)
else
super()
end
end
end
puts Child.new.greet
puts Child.new.greet("alice")Solution
Step 1: Analyze Child#greet with no argument
When called without argument,nameis nil, sosuper()calls Parent#greet with no arguments, using default "Guest". When called with "alice",name.upcaseis "ALICE" passed tosuper(name.upcase), so Parent#greet returns "Hello, ALICE from Parent".Final Answer:
Hello, Guest from Parent Hello, ALICE from Parent -> Option BQuick Check:
super() calls parent with defaults; super(args) passes modified args [OK]
- Assuming super() passes current args
- Ignoring default parameter in Parent#greet
- Not uppercasing the argument before passing
Ready to prove it?
Pass the test and this pattern moves unknown
