Implicit return (last expression) in Ruby - Time & Space Complexity
Let's see how the time it takes to run a Ruby method depends on what it does inside.
We want to know how the method's steps grow as the input changes.
Analyze the time complexity of the following code snippet.
def sum_array(arr)
total = 0
arr.each do |num|
total += num
end
total
end
This method adds up all numbers in an array and returns the total using implicit return of the last expression.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element of the array.
- How many times: Once for every item in the array.
As the array gets bigger, the method does more additions, one for each item.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the input size.
[X] Wrong: "Since the method returns the last line, it runs only once regardless of input size."
[OK] Correct: The last line is just the result, but the loop before it runs once for each item, so the total work depends on input size.
Understanding how loops affect time helps you explain code efficiency clearly and confidently.
"What if we replaced the loop with a recursive call? How would the time complexity change?"
Practice
return statement?Solution
Step 1: Understand Ruby method return behavior
Ruby methods automatically return the value of the last expression evaluated if no explicitreturnis used.Step 2: Apply this to the question
Since no explicitreturnis given, the method returns the last expression's value.Final Answer:
The value of the last expression evaluated in the method -> Option AQuick Check:
Implicit return = last expression value [OK]
- Thinking Ruby returns nil by default
- Assuming explicit return is always needed
- Confusing first and last expression return
Solution
Step 1: Check each method for implicit return usage
def add(a, b) a + b end returns the last expressiona + bimplicitly withoutreturn.Step 2: Analyze other options
def add(a, b) return a + b puts 'done' end uses explicitreturn, def add(a, b) puts a + b end returnsnilbecauseputsreturns nil, def add(a, b) a + b; return end hasreturnwith no value, returning nil.Final Answer:
def add(a, b) a + b end -> Option AQuick Check:
Implicit return = last expression value [OK]
- Confusing puts output with return value
- Using return unnecessarily
- Returning nil unintentionally
def greet(name)
message = "Hello, "+name
message.upcase
end
puts greet("Alice")Solution
Step 1: Trace method execution
The method createsmessageas "Hello, Alice" then callsmessage.upcasewhich returns "HELLO, ALICE".Step 2: Determine return value
Sincemessage.upcaseis the last expression, it is implicitly returned.Final Answer:
"HELLO, ALICE" -> Option DQuick Check:
Last expression returned = "HELLO, ALICE" [OK]
- Assuming original message is returned
- Thinking puts output is return value
- Confusing method return with print output
def multiply(x, y) product = x * y puts product end result = multiply(3, 4) puts result
Solution
Step 1: Analyze last expression in method
The last expression isputs product, which returns nil.Step 2: Understand method return value
Because Ruby returns the last expression implicitly, the method returns nil, not the product.Final Answer:
Method returns nil because last expression is puts -> Option CQuick Check:
Implicit return = last expression value (puts returns nil) [OK]
- Expecting method to return printed value
- Thinking missing return causes syntax error
- Confusing output with return value
def check_value(val)
if val > 10
"High"
else
"Low"
end
end
check_value(10)Solution
Step 1: Evaluate the condition with val = 10
Since 10 is not greater than 10, the else branch executes returning "Low".Step 2: Understand implicit return in if-else
The last evaluated expression is the string "Low", which is implicitly returned by the method.Final Answer:
"Low" -> Option BQuick Check:
Implicit return of last expression in if-else = "Low" [OK]
- Assuming val > 10 includes 10
- Expecting explicit return needed
- Confusing nil with else return
Ready to prove it?
Pass the test and this pattern moves unknown
