On Fri, Sep 9, 2011 at 1:15 PM, rahul patil
<rahul.deshmukhpatil / gmail.com> wrote:
> On Thu, Sep 8, 2011 at 8:54 PM, Michael Kohl <citizen428 / gmail.com> wrote:
>> class Array
>>   
>> 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 , 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