From: rahul patil Date: 2011-09-09T14:15:53+09:00 Subject: Re: Grouping an array into sub-arrays. --20cf303bf95653592204ac7b4549 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Sep 8, 2011 at 8:54 PM, Michael Kohl wrote: > On Wed, Sep 7, 2011 at 12:47 AM, Intransition wrote: > > Is there a method out there already that does anything like this? > > > > [0,1,2,3,4].group(0..-2, -1) #=> [[0,1,2,3]. [4]] > > [0,1,2,3,4].group(0, 2..3, 4..-1) #=> [[0], [2,3], [4]] > > I guess I would just do something like this: > > yes it does works vey well, nice code snippet. > class Array > def group(*ranges) > ranges.map { |r| values_at(r) } > end > end > > >> [0,1,2,3,4].group(0..-2, -1) > => [[0, 1, 2, 3], [4]] > >> [0,1,2,3,4].group(0, 2..3, 4..-1) #=> [[0], [2,3], [4]] > can we have something so that if we pass array variable name instead of actual elements of array like 0, 2..3, 4..-1 i mean if range=[ 0, 2..3, 4..-1], array= [0,1,2,3,4] then array.group(range), should give same o/p => [[0], [2, 3], [4]] > > -- Regards, Rahul Patil --20cf303bf95653592204ac7b4549--