From: botp Date: 2011-09-09T15:56:05+09:00 Subject: Re: Grouping an array into sub-arrays. On Fri, Sep 9, 2011 at 1:15 PM, rahul patil wrote: > On Thu, Sep 8, 2011 at 8:54 PM, Michael Kohl wrote: >> 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]] try refining Michael's code. eg, class Array def group *ranges ranges.flatten.map{|r| values_at r} end end #=> nil [0,1,2,3,4].group 0,2..3,4..-1 #=> [[0], [2, 3], [4]] [0,1,2,3,4].group [0,2..3,4..-1] #=> [[0], [2, 3], [4]] [0,1,2,3,4].group r=[0,2..3,4..-1] #=> [[0], [2, 3], [4]] [0,1,2,3,4].group r #=> [[0], [2, 3], [4]] [0,1,2,3,4].group *r #=> [[0], [2, 3], [4]] best regards -botp