tags:

views:

39

answers:

1

I have an array of objects @objects and would like to split this into an array of arrays based on a parameter, ending up with an array where each entry is an array of objects all of which have object.property the same.

@objects = [obj1, obj2, obj3, obj4, obj5]
obj1.property = a
obj2.property = a
obj3.property = b
obj4.property = b
obj5.property = c
array = [[obj1, obj2,], [obj3, obj4], [obj5]]
+3  A: 
@objects.group_by { |obj| obj.property }.values
lest
You'll have to be on ruby 1.9 for this (otherwise the array may be in a wrong order).
steenslag
Samuel
Got ruby 1.9.2p0, works perfectly thank you. Must look harder next time!