times / upto / each
Numeric and collection loops.
Syntax
n.times { |i| ... }Description
n.times loops 0..n-1; (1..n).each iterates over a range; arr.each iterates over elements. These are preferred over for in Ruby.
Example
Loading editor…
Expected output
0 1 2 10 20 30
When to use?
Specific repetition, list traversal.