Each_slice()

I’m using the following code:

some_array.each_slice(2).to_a

Now, I have a problem. I distribute my code as a gem, and I need to know
from which version Ruby supports this each_slice() syntax (I’ll put this
version number in my gem specification: s.required_ruby_version = ‘=>
1.8.?’).

I located this feature in MRI Ruby’s ChangeLog, but when I use git to
see which versions (tags) contain this commit, I see that it’s only in
the 1.9.x versions. This couldn’t be, of course, because this feature is
somehow in 1.8.x too.

If you where using each_slice(), what versions of Ruby would you limit
your gem to?

Albert S. wrote:

If you where using each_slice(), what versions of Ruby would you limit
your gem to?

I searched the net and it seems it’s only safe to use each_slice() in
Ruby 1.9, I think. But I need my code to work in 1.8 as well.

Anybody has an idea how to rewrite the following code

some_array.each_slice(2).to_a

so that it works in Ruby 1.8 too?

Albert S. wrote:

Anybody has an idea how to rewrite the following code

some_array.each_slice(2).to_a

so that it works in Ruby 1.8 too?

Problem solved. I eventually wrote this:

some_array = [1,2,3,4,5,6]

pairs = []
pairs << some_array.shift(2) while !some_array.empty?

Rick Denatale wrote:

so that it works in Ruby 1.8 too?

require ‘enumerator’

ruby -v
ruby 1.8.7 (2009-12-24 patchlevel 248)

irb

irb(main):001:0> require ‘enumerator’
=> false
irb(main):002:0> (1…6).to_a.each_slice(2).to_a
=> [[1, 2], [3, 4], [5, 6]]

But how is your answer helpful?

I know each_slice() works in some versions of 1.8. It works on my 1.8
too. My question was: starting with which version?

Albert S. wrote:

But how is your answer helpful?

I know each_slice() works in some versions of 1.8. It works on my 1.8
too. My question was: starting with which version?

Let me be more elaborate:

each_slice() was introduced in 1.9. It was then backported to 1.8.
That’s how i understand what I read on the internet (I may be wrong).

What I was trying to do was to find out, for example, if all “1.8.7”
versions have each_slice() and can be called without a block (so that I
could add “>= 1.8.7” as a dependency to my gem).

When you say “ruby 1.8.7 (2009-12-24 patchlevel 248)” has this
each_slice(), it doesn’t yet help me.

On Wed, Mar 31, 2010 at 6:45 PM, Albert S. [email protected]
wrote:

so that it works in Ruby 1.8 too?

require ‘enumerator’

ruby -v
ruby 1.8.7 (2009-12-24 patchlevel 248)

irb

irb(main):001:0> require ‘enumerator’
=> false
irb(main):002:0> (1…6).to_a.each_slice(2).to_a
=> [[1, 2], [3, 4], [5, 6]]


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

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

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.

On Wed, Mar 31, 2010 at 7:47 PM, Albert S. [email protected]
wrote:

What I was trying to do was to find out, for example, if all “1.8.7”
versions have each_slice() and can be called without a block (so that I
could add “>= 1.8.7” as a dependency to my gem).

When you say “ruby 1.8.7 (2009-12-24 patchlevel 248)” has this
each_slice(), it doesn’t yet help me.

Posted via http://www.ruby-forum.com/.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Robert K. wrote:

I have enough confidence that if not all at least most versions
(especially newer versions) have that feature. So I would place 1.8.7
as dependency. I’d say you are pretty safe since the newest versions
definitively have it and the percentage of old 1.8.7 versions
installed is probably rather low. My 0.02EUR.

Thanks.

In future projects I write I’ll just make “>= 1.8.7” a dependency and
assume the world is a very nice place.

(I’ve just looked into DataMapper’s gemspec and I see they don’t put a
dependency on a certain version on Ruby. As if they don’t care. But
surely they don’t support old Rubys. I guess that’s their prerogative
(thanks Britny!). They’re big so they don’t care. I, on the other hand,
am small, so I need to bother myself with these details.)

2010/4/1 Albert S. [email protected]:

What I was trying to do was to find out, for example, if all “1.8.7”
versions have each_slice() and can be called without a block (so that I
could add “>= 1.8.7” as a dependency to my gem).

When you say “ruby 1.8.7 (2009-12-24 patchlevel 248)” has this
each_slice(), it doesn’t yet help me.

I have enough confidence that if not all at least most versions
(especially newer versions) have that feature. So I would place 1.8.7
as dependency. I’d say you are pretty safe since the newest versions
definitively have it and the percentage of old 1.8.7 versions
installed is probably rather low. My 0.02EUR.

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs