Why methods always return a value in Ruby - Performance Analysis
When we look at Ruby methods, they always give back a result. Understanding how this affects the time it takes to run helps us write better code.
We want to see how the work done inside a method grows as the input changes.
Analyze the time complexity of the following code snippet.
def sum_array(numbers)
total = 0
numbers.each do |num|
total += num
end
total
end
This method adds up all numbers in an array and returns the total sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the array.
- How many times: Once for every number in the input array.
As the array gets bigger, the method does more additions, one for each number.
| 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. Double the items, double the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the input size.
[X] Wrong: "The method returns instantly because it just returns a value."
[OK] Correct: Even though a method returns a value, it still does all the work inside first, which takes time depending on input size.
Knowing how method return values relate to the work done inside helps you explain code efficiency clearly and confidently.
"What if we changed the method to return the sum of only the first half of the array? How would the time complexity change?"
Practice
return statement?Solution
Step 1: Understand Ruby method return behavior
Ruby methods do not require an explicitreturnstatement to send back a value.Step 2: Identify what value is returned
The value of the last executed line inside the method is automatically returned.Final Answer:
Because Ruby automatically returns the value of the last executed line in the method. -> Option DQuick Check:
Last line value returned [OK]
- Thinking a return statement is always needed
- Believing methods return nil if no return is used
- Assuming the first line's value is returned
return?Solution
Step 1: Check method syntax without explicit return
def add(a, b) a + b end defines a method where the last line isa + b, which will be returned automatically.Step 2: Compare other options
def add(a, b) return a + b end uses explicit return (also correct but not asked). def add(a, b) puts a + b end prints the sum but returns nil. def add(a, b) a + b; nil end returns nil explicitly.Final Answer:
def add(a, b) a + b end -> Option BQuick Check:
Last line expression returned [OK]
- Using puts instead of returning value
- Adding explicit nil at the end
- Confusing printing with returning
def greet(name)
"Hello, #{name}!"
42
end
puts greet("Alice")Solution
Step 1: Analyze method body lines
The method has two lines: first creates a string but does not return it explicitly; second line is the number 42.Step 2: Identify returned value
Ruby returns the last evaluated expression, which is 42.Final Answer:
42 -> Option CQuick Check:
Last line value returned = 42 [OK]
- Assuming first string line is returned
- Confusing puts output with return value
- Expecting nil because no explicit return
nil instead. What is the error?def finish puts "Done" end result = finish puts result.inspect
Solution
Step 1: Understand what
putsreturnsputsprints to screen but returns nil.Step 2: Identify method return value
Sinceputs "Done"is the last line, method returns nil.Final Answer:
The method usesputswhich returns nil, so method returns nil. -> Option AQuick Check:
puts returns nil [OK]
- Thinking puts returns the printed string
- Believing explicit return is always needed
- Confusing print and puts behavior
def first_non_nil(a, b) # fill in end
Solution
Step 1: Understand the goal
We want the method to return the first non-nil value betweenaandb.Step 2: Check each option's return value
def first_non_nil(a, b) a || b end returnsa || bwhich evaluates to first non-nil value automatically. def first_non_nil(a, b) return a if a return b end uses explicit returns but is longer. def first_non_nil(a, b) puts a || b end prints but returns nil. def first_non_nil(a, b) a b end returnsbalways because last line isb.Final Answer:
def first_non_nil(a, b) a || b end -> Option AQuick Check:
Last expression returns first non-nil [OK]
- Using puts instead of returning value
- Writing multiple return statements unnecessarily
- Returning wrong line's value
Ready to prove it?
Pass the test and this pattern moves unknown
