Accessing parent methods in Ruby - Time & Space Complexity
When a child class calls a method from its parent class, it runs some code inherited from above. We want to see how the time it takes changes as the input grows.
How does calling a parent method affect the total work done?
Analyze the time complexity of the following code snippet.
class Parent
def process(items)
items.each do |item|
puts item * 2
end
end
end
class Child < Parent
def process(items)
super
end
end
child = Child.new
child.process([1, 2, 3, 4, 5])
This code defines a parent class with a method that loops over a list and prints doubled values. The child class calls the parent's method using super.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each item in the list inside the parent method.
- How many times: Once for each item in the input array.
As the list gets bigger, the method runs the loop more times, doing more work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times printing doubled values |
| 100 | 100 times printing doubled values |
| 1000 | 1000 times printing doubled values |
Pattern observation: The work grows directly with the number of items. Double the items, double the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of items in the list.
[X] Wrong: "Calling super adds extra loops or doubles the work."
[OK] Correct: The child just calls the parent's method once. It does not repeat the loop multiple times, so the work stays proportional to the input size.
Understanding how inherited methods run helps you explain code efficiency clearly. It shows you can think about how code layers affect performance, a useful skill in many coding tasks.
What if the child method added another loop over the items before calling super? How would the time complexity change?
Practice
Solution
Step 1: Understand method inheritance in Ruby
Child classes inherit methods from parent classes but to call the parent's version inside an overridden method, you usesuper.Step 2: Identify the correct syntax for calling parent method
Usingsuperinside the child method calls the parent method with the same name.Final Answer:
Usesuperinside the child method -> Option DQuick Check:
Calling parent method = super [OK]
super to call parent method inside child [OK]- Trying to call parent method by name directly
- Using non-existent syntax like
parent.method_name - Confusing
thiswith Ruby'sself
Solution
Step 1: Recall Ruby method call syntax
In Ruby, method arguments are passed separated by commas without brackets unless parentheses are used.Step 2: Identify correct
The correct way issuperusage with argumentssuper arg1, arg2orsuper(arg1, arg2). super arg1, arg2 uses the correct syntax without parentheses.Final Answer:
super arg1, arg2 -> Option AQuick Check:
Pass args to super with commas [OK]
- Using brackets [] or braces {} instead of parentheses or commas
- Omitting commas between arguments
- Using incorrect syntax like super(arg1 arg2)
class Parent
def greet(name)
"Hello, #{name} from Parent"
end
end
class Child < Parent
def greet(name)
super(name) + " and Child"
end
end
puts Child.new.greet("Alice")Solution
Step 1: Understand method overriding and super call
TheChild#greetmethod callssuper(name), which callsParent#greet(name)and returns "Hello, Alice from Parent".Step 2: Concatenate the returned string
The child method adds " and Child" to the parent's returned string, resulting in "Hello, Alice from Parent and Child".Final Answer:
"Hello, Alice from Parent and Child" -> Option BQuick Check:
super returns parent's string + child's addition [OK]
- Thinking super calls child's method again causing recursion
- Ignoring the concatenation after super call
- Assuming super without arguments forwards none
class Parent
def info
"Parent info"
end
end
class Child < Parent
def info
super()
puts "Child info"
end
end
Child.new.infoSolution
Step 1: Analyze the super call with parentheses
Callingsuper()calls the parent method with no arguments and returns "Parent info" but does not print it.Step 2: Check what is printed
The child method prints only "Child info" because the parent's return value is ignored.Final Answer:
super() returns value but is ignored; only child info prints -> Option AQuick Check:
super() returns but doesn't print [OK]
- Assuming super() prints parent's return value automatically
- Thinking super() without args causes error
- Confusing syntax error with runtime behavior
class A
def message
"A"
end
end
class B < A
def message
"B" + super
end
end
class C < B
def message
super + "C"
end
end
puts C.new.messageSolution
Step 1: Trace method calls from C to A
C#messagecallssuperwhich isB#message.B#messagereturns "B" +superwhich callsA#messagereturning "A".Step 2: Combine returned strings step-by-step
B#messagereturns "B" + "A" = "BA". ThenC#messagereturns "BA" + "C" = "BAC".Final Answer:
"BAC" -> Option CQuick Check:
super chains add strings in order [OK]
- Mixing order of concatenation
- Assuming super calls only immediate parent
- Ignoring that super returns parent's result
Ready to prove it?
Pass the test and this pattern moves unknown
