First let’s define an Infinity constant (since Ruby does not come with one):
Inf = 1.0 / 0.0
Now let’s see if we can create a Range Object with it:
(1..Inf)
Okay, no errors. Now let’s try to use it:
(1..Inf).include?(10000000) #=> true
(1..Inf).include?(-10) #=> false
(-Inf..0).include?(-1000) #=> true
(-Inf..Inf).include?(-1000.456) #=> true
Now let’s try using it as a ‘lazy’ list:
(1..Inf).each.take(5) #=> [1, 2, [...]
Filed under: programming, ruby | Tagged: ruby Infinity Lazy | 7 Comments »