Enumerators can only "move" in one direction - forward. #end would be
useless for many (for ex. (1..Float::INFINITY).each - this one never
ends), providing #prev would be equivalent to remembering all of
values yielded so far (unpractical, we don't have infinite memory).
-- Matma Rex
···
2012/3/12 Ralph Shnelvar <ralphs@dos32.com>:
I see that enumerators have the methods rewind and next.
Are there equivalent methods "end" and "prev"?
I see reverse_each ... but what if I do have a large enumerable object and don't want the intermediate array created?
Builds a temporary array and traverses that array in reverse order.
If no block is given, an enumerator is returned instead.
- - -
Enumerators can only "move" in one direction - forward. #end would be
useless for many (for ex. (1..Float::INFINITY).each - this one never
ends), providing #prev would be equivalent to remembering all of
values yielded so far (unpractical, we don't have infinite memory).
-- Matma Rex
For ranges, all you need define is the inverse of the succ function (which must be defined in order to "go forwards").
By the way,
(99_999_999_990..100_000_000_000).last(3) works just fine but
(1..100_000_000_000) seems to hang in a loop. I suspect Ruby is constructing a temporary array.