Concept Flow - Times method for counted repetition
Start with number n
Call n.times do |i|
Execute block with i from 0 to n-1
Repeat until i == n-1
Exit loop
The times method runs a block of code n times, passing the current count from 0 up to n-1.
3.times do |i| puts i end
| Iteration | i (block variable) | Action | Output |
|---|---|---|---|
| 1 | 0 | Execute block with i=0 | 0 |
| 2 | 1 | Execute block with i=1 | 1 |
| 3 | 2 | Execute block with i=2 | 2 |
| Exit | - | i reached 3, loop ends | - |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| i | - | 0 | 1 | 2 | - |
times method syntax: number.times do |i| # code using i end Runs block n times with i from 0 to n-1. Useful for counted repetition without manual loops. Block variable i starts at 0, ends at n-1.