Block parameters in Ruby - Time & Space Complexity
When using block parameters in Ruby, it's important to see how the code runs as the input grows.
We want to know how the number of steps changes when we pass more items to the block.
Analyze the time complexity of the following code snippet.
numbers = [1, 2, 3, 4, 5]
numbers.each do |num|
puts num * 2
end
This code goes through each number in the list and prints double its value using a block with a parameter.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
eachmethod loops over every item in the array. - How many times: Once for each item in the array, so as many times as the array length.
As the list gets bigger, the number of times the block runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times the block runs |
| 100 | 100 times the block runs |
| 1000 | 1000 times the block runs |
Pattern observation: The work grows directly with the number of items; double the items means double the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of items you process.
[X] Wrong: "Using a block with parameters makes the code run faster or slower than normal loops."
[OK] Correct: The block parameters just pass each item to the block; they don't change how many times the loop runs or how long it takes per item.
Understanding how blocks with parameters work helps you explain how Ruby handles repeated tasks clearly and efficiently.
"What if we replaced each with map? How would the time complexity change?"
Practice
What are block parameters in Ruby?
Choose the best description.
Solution
Step 1: Understand block parameters role
Block parameters are variables placed inside| |afterdoor{to receive values passed into the block.Step 2: Compare options with definition
Only Variables inside| |that receive values passed to the block. correctly describes block parameters as variables inside| |that get values passed to the block.Final Answer:
Variables inside | | that receive values passed to the block. -> Option AQuick Check:
Block parameters = variables inside | | [OK]
- Confusing block parameters with methods
- Thinking block parameters are keywords
- Assuming block parameters are constants
Which of the following is the correct syntax for block parameters in Ruby?
array.each do ?
Solution
Step 1: Recall block parameter syntax
Block parameters are placed inside vertical bars| |afterdoor{.Step 2: Match syntax with options
Only |item| puts item uses|item|which is the correct syntax for block parameters.Final Answer:
|item| puts item -> Option CQuick Check:
Block parameters use | | [OK]
- Using parentheses () instead of | |
- Using curly braces {} for parameters
- Using square brackets [] for parameters
What is the output of this Ruby code?
numbers = [1, 2, 3] numbers.each do |n| puts n * 2 end
Solution
Step 1: Understand the block parameter usage
The block parameterntakes each element from the arraynumbersone by one.Step 2: Calculate output for each element
For eachn, the code printsn * 2. So outputs are 2, 4, and 6 on separate lines.Final Answer:
2 4 6 -> Option AQuick Check:
Each number doubled = 2,4,6 [OK]
- Confusing block parameter with array variable
- Expecting original numbers printed
- Thinking block parameter is undefined
Find the error in this Ruby code:
fruits = ['apple', 'banana'] fruits.each do fruit| puts fruit end
Solution
Step 1: Check block parameter syntax
Block parameters must be enclosed inside vertical bars| |. Here, only the closing bar is present.Step 2: Identify missing part
The opening|beforefruitis missing, causing a syntax error.Final Answer:
Missing opening | before block parameter -> Option DQuick Check:
Block parameters need both | | [OK]
- Forgetting the opening |
- Using parentheses instead of | |
- Assuming code runs without |
Given this code, what will be the output?
pairs = [[1, 2], [3, 4], [5, 6]] pairs.each do |a, b| puts a + b end
Solution
Step 1: Understand multiple block parameters
The block parametersaandbreceive each inner array's two elements separately.Step 2: Calculate sum for each pair
For each pair, the code prints the sum: 1+2=3, 3+4=7, 5+6=11.Final Answer:
3 7 11 -> Option BQuick Check:
Sum of pairs = 3,7,11 [OK]
- Expecting array printed instead of sum
- Thinking block parameters can't unpack arrays
- Assuming syntax error with two parameters
Ready to prove it?
Pass the test and this pattern moves unknown
