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
>> =A0def group(*ranges)
>> =A0 =A0 ranges.map { |r| values_at(r) }
>> =A0 end
>> end
>> >> [0,1,2,3,4].group(0..-2, -1)
>> =3D> [[0, 1, 2, 3], [4]]
>> >> [0,1,2,3,4].group(0, 2..3, 4..-1) =A0#=3D> [[0], [2,3], [4]]
> can we have something so that if we pass array variable name instead of
> actual elements of array like =A00, 2..3, 4..-1
> i mean if range=3D[ 0, 2..3, 4..-1], array=3D [0,1,2,3,4]
> then array.group(range), should give same o/p
>
> =3D> [[0], [2, 3], [4]]

try refining Michael's code.
eg,

class Array
  def group *ranges
    ranges.flatten.map{|r| values_at r}
  end
end
#=3D> nil

[0,1,2,3,4].group 0,2..3,4..-1
#=3D> [[0], [2, 3], [4]]
[0,1,2,3,4].group [0,2..3,4..-1]
#=3D> [[0], [2, 3], [4]]
[0,1,2,3,4].group r=3D[0,2..3,4..-1]
#=3D> [[0], [2, 3], [4]]
[0,1,2,3,4].group r
#=3D> [[0], [2, 3], [4]]
[0,1,2,3,4].group *r
#=3D> [[0], [2, 3], [4]]

best regards -botp