Prev: Weird hash key: what am I asking Ruby to do?
Next: stetecting change in number of leading spaces while looping
From: Albert Schlef on 31 Mar 2010 19:41 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? -- Posted via http://www.ruby-forum.com/.
From: Rick DeNatale on 31 Mar 2010 21:01 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 Schlef <albertschlef(a)gmail.com> wrote: > 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/. > > -- 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 31 Mar 2010 21:43 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 -- Posted via http://www.ruby-forum.com/.
From: Robert Klemme on 1 Apr 2010 03:57 2010/4/1 Albert Schlef <albertschlef(a)gmail.com>: > 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. 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Albert Schlef on 1 Apr 2010 21:54
Robert Klemme 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.) -- Posted via http://www.ruby-forum.com/. |