Times method for counted repetition in Ruby - Time & Space Complexity
We want to understand how the time it takes to run code changes when we use Ruby's times method.
Specifically, how does repeating an action a certain number of times affect the total work done?
Analyze the time complexity of the following code snippet.
5.times do |i|
puts "Count: #{i}"
end
This code repeats a simple print action 5 times, counting from 0 to 4.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The block inside
timesruns once per count. - How many times: Exactly as many times as the number given to
times(here, 5 times).
When the number given to times grows, the number of repeated actions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 actions |
| 100 | 100 actions |
| 1000 | 1000 actions |
Pattern observation: The total work grows directly in step with the input number.
Time Complexity: O(n)
This means if you double the number of times, the work roughly doubles too.
[X] Wrong: "The times method runs in constant time no matter the number."
[OK] Correct: Each repetition runs the block once, so more repetitions mean more work.
Understanding how repeating actions affect time helps you explain how loops work in real code.
"What if we replaced times with a nested times inside another times? How would the time complexity change?"
Practice
times method do when called on an integer?Solution
Step 1: Understand the
Thetimesmethod purposetimesmethod repeats a block of code a specified number of times, starting from zero.Step 2: Identify what happens when called on an integer
When called on an integer, it runs the block that many times, passing the current count (starting at zero) to the block.Final Answer:
Repeats the given block of code that many times starting from zero. -> Option AQuick Check:
5.times { |i| puts i }prints 0 to 4 [OK]
times runs code block count times from zero [OK]- Thinking it returns an array
- Assuming it starts counting from 1
- Confusing it with range creation
times in Ruby?Solution
Step 1: Check Ruby
The correct syntax is callingtimessyntaxtimeson an integer with a block, either withdo...endor curly braces.Step 2: Identify the correct block parameter usage
5.times do |i| puts i end uses5.times do |i| puts i end, which is valid Ruby syntax to print 0 to 4.Final Answer:
5.times do |i| puts i end -> Option BQuick Check:
Correct block syntax = 5.times do |i| puts i end [OK]
integer.times do |i| ... end for correct syntax [OK]- Using
times(5)which is invalid - Incorrect block syntax like
{ i in ... } - Omitting block braces or do/end
3.times { |i| puts i * 2 }Solution
Step 1: Understand the iteration count and block variable
The3.timesruns the block 3 times withivalues 0, 1, 2.Step 2: Calculate the output for each iteration
Each iteration printsi * 2: 0*2=0, 1*2=2, 2*2=4.Final Answer:
0 2 4 -> Option AQuick Check:
Multiply index by 2 = 0, 2, 4 [OK]
- Assuming index starts at 1
- Printing index without multiplication
- Confusing output order
5.times do i puts i end
Solution
Step 1: Check block parameter syntax
In Ruby, block parameters must be enclosed in pipes:do |i| ... end.Step 2: Identify the missing pipes causing syntax error
The code usesdo iwithout pipes, which is invalid syntax.Final Answer:
Missing block parameter pipes | | around i -> Option CQuick Check:
Block params need |i|, not just i [OK]
- Omitting pipes around block parameters
- Thinking do/end can't be used with times
- Misunderstanding times method usage
times. Which code correctly does this?Solution
Step 1: Understand how to collect results in an array
Sincetimesreturns the integer, not an array, we must create an empty array and push results inside the block.Step 2: Check each option for correct syntax and logic
squares = [] 5.times { |i| squares << i * i } puts squares initializes an empty array, uses5.timeswith block parameteri, and appendsi*ito the array correctly.Final Answer:
squares = [] 5.times { |i| squares << i * i } puts squares -> Option DQuick Check:
Use array and push inside times block [OK]
- Assuming times returns an array
- Using invalid range with times
- Omitting block parameter pipes
Ready to prove it?
Pass the test and this pattern moves unknown
