That code will produce the right result but will read the whole file, which is not what is desired
Indeed, each_slice currently does not return a lazy enumerator :-(
To make the above code as intended, one must call .lazy right after the each_slice(3). I feel this is dangerous and counter intuitive.
Is there a valid reason for this behavior? Otherwise, I would like us to consider returning a lazy enumerator for the following methods:
(when called without a block)
each_with_object
each_with_index
each_slice
each_entry
each_cons
(always)
chunk
slice_before
The arguments are:
fail early (much easier to realize one needs to call a final force, to_a or each than realizing that a lazy enumerator chain isn't actually lazy)
easier to remember (every method normally returning an enumerator returns a lazy enumerator). basically this makes Lazy covariant
I'd expect that if you get lazy at some point, you typically want to remain lazy until the very end
I guess these methods were forgotten to change when lazy was implemented.
That's right :-( I thought these methods does not need to be overriden
because they return Enumerator, but they should return Enumerator::Lazy for such cases.
This issue was solved with changeset r39058.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
enumerator.c: Use to_enum for Enumerable methods returning Enumerators.
This makes Lazy#cycle no longer needed, so it was removed.
Make Enumerator#chunk and slice_before return lazy Enumerators.
[Bug #7715]
internal.h: Remove ref to rb_enum_cycle_size; no longer needed