Rick Denatale wrote:
No,
each_slice is/was in Ruby 1.8, but it is defined as part of the
enumerator extension which needs to be required in order to add
each_slice and several other instance methods to the enumerable
module. It wasn’t backported from 1.9, rather 1.9 made the extension
a standard part of the Enumerable class.
So requiring enumerator should work for any version of 1.8.x, or at
least back to 1.8.2 which is the version documented by the 2nd edition
of the Pickaxe.
Rick, thank you for this info. Much of this is new to me.
I still have a problem. Look:
some_array.each_slice(2).to_a
I’m not passing a block to each_slice(). This feature, of not passing a
block, is “relatively” new. That’s a problem. I need to know form which
version of ruby it is supported.
Let me start from the beginning.
My code originally was this:
some_array.enum_slice(2).to_a
It works on Ruby 1.8.
But I want my code to work on Ruby 1.9 too, and in Ruby 1.9 there’s no
more enum_slice(). Instead, one uses each_slice(). So I changed my code
to…
some_array.each_slice(2).to_a
…and it works fine on 1.9. But… it won’t work on older versions of
1.8, because their each_slice() must get a block.
So… how can I make my code work on both 1.9 and older versions of 1.8?
UPDATE
Hey, I think I have a solution!! What about the following line?
some_array.enum_for(:each_slice, 2).to_a