Do something infinitely many times with an index
There’s a predefined (in 1.9.2) constant Float::INFINITY
, so you could write
1.upto(Float::INFINITY) do |i|
...
end
(You could also use Enumerator
and take_while
, turning the problem inside out to make it look more like Haskell or Python, but take_while
is greedy and builds an array.)
Ruby 2.5 introduced the open-ended Range:
(1..).each do |i|
#...
end
Numeric.step has default parameters of infinity (the limit) and 1 (the step size).
1.step do |i|
#...
end
For fun, you might even want to try
1.step.size