Block syntax (do..end and curly braces) in Ruby - Time & Space Complexity
When using blocks in Ruby, it's important to see how the code inside runs as input grows.
We want to know how the number of times the block runs changes with input size.
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.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The block inside
eachruns once for every item in the array. - How many times: Exactly as many times as there are elements in the array.
As the list gets bigger, the block runs more times, directly matching the list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 block executions |
| 100 | 100 block executions |
| 1000 | 1000 block executions |
Pattern observation: The number of operations grows in a straight line with input size.
Time Complexity: O(n)
This means the time to run grows directly with how many items are in the list.
[X] Wrong: "Using curly braces instead of do..end changes how many times the block runs."
[OK] Correct: Both block styles run the same number of times; the difference is only in syntax and precedence, not performance.
Understanding how blocks run helps you explain how your code scales and shows you know how Ruby handles repeated actions.
"What if we replaced each with map? How would the time complexity change?"
Practice
Solution
Step 1: Understand block syntax options
Ruby supports two block syntaxes:do..endand curly braces{ }.Step 2: Identify usage for multi-line blocks
do..endis preferred for multi-line blocks because it improves readability.Final Answer:
do..end-> Option DQuick Check:
Multi-line blocks use do..end [OK]
- Using curly braces for multi-line blocks reduces readability
- Thinking Ruby doesn't support blocks
- Believing both syntaxes are identical in all cases
Solution
Step 1: Identify correct block syntax for single-line
Single-line blocks use curly braces{ }with parameters inside pipes| |.Step 2: Check syntax correctness
[1, 2, 3].each { |n| puts n } correctly uses{ |n| puts n }. Others have syntax errors.Final Answer:
[1, 2, 3].each { |n| puts n } -> Option AQuick Check:
Single-line block with braces and pipes [OK]
- Missing pipes around block parameters
- Using do..end without proper line breaks
- Incorrect placement of pipes or braces
(1..3).map { |x| x * 2 }Solution
Step 1: Understand the range and map method
The range (1..3) produces numbers 1, 2, 3. Themapmethod applies the block to each number.Step 2: Calculate each mapped value
Each number is multiplied by 2, resulting in 2, 4, and 6.Final Answer:
[2, 4, 6] -> Option BQuick Check:
Map doubles each element [OK]
- Confusing map with each (which returns original array)
- Expecting a syntax error due to block syntax
- Misunderstanding range behavior
5.times do |i| puts i }
Solution
Step 1: Check block opening and closing syntax
The block starts withdobut incorrectly ends with a curly brace}.Step 2: Understand correct block closure
Blocks opened withdomust close withend, not}.Final Answer:
Incorrect use of curly brace to close a do..end block -> Option CQuick Check:
do..end blocks cannot close with braces [OK]
- Mixing do..end with curly braces
- Forgetting to close the block
- Misplacing block parameter pipes
[1, 2, 3, 4], which block syntax correctly filters even numbers and returns them as a new array?Solution
Step 1: Understand the select method and block parameters
selectfilters elements for which the block returns true. The block must receive each element as a parameter.Step 2: Check syntax correctness
[1, 2, 3, 4].select { |n| n.even? } correctly uses curly braces with block parameter|n|and callsn.even?. [1, 2, 3, 4].select do |n| n.even? has a syntax error (missingend). [1, 2, 3, 4].select { n.even? } misses block parameters. [1, 2, 3, 4].select do n.even? end misses pipes around parameter.Final Answer:
[1, 2, 3, 4].select { |n| n.even? } -> Option AQuick Check:
Curly braces with pipes for single-line filter [OK]
- Omitting block parameters
- Mixing do..end without pipes for parameters
- Using incorrect block syntax causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
