From: Albert Schlef on
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?
--
Posted via http://www.ruby-forum.com/.

From: Albert Schlef on
Albert Schlef 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?
--
Posted via http://www.ruby-forum.com/.

From: Albert Schlef on
Albert Schlef 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?
--
Posted via http://www.ruby-forum.com/.

From: Rick DeNatale on
On Wed, Mar 31, 2010 at 6:45 PM, Albert Schlef <albertschlef(a)gmail.com> wrote:
> Albert Schlef 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?


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

From: Albert Schlef on
Albert Schlef 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.
--
Posted via http://www.ruby-forum.com/.