From: Rick DeNatale Date: 2008-01-21T22:17:07+09:00 Subject: Re: Quick way to split up an array of objects into groups? On Jan 21, 2008 8:07 AM, Max Williams wrote: > Xavier Noria wrote: > > On Jan 21, 2008, at 12:23 PM, Max Williams wrote: > > > >> > >> Can anyone tell me a nice and simple way of doing this? > > > > Enumerable#parition or Set#classify might help. > > > > -- fxn > > hmmm...i couldn't work out how to make it easier with either of those. > I ended up doing this, which isn't very clever but it did seem to work. > > groups = [] > vals = collection.collect{|obj| [obj.val1, obj.val2]}.uniq! > for val in vals > groups << collection.select{|obj| obj.val1 == val[0] && obj.val2 = > val[1] > end The activesupport gem, which is part of Rails, but can be used separately, adds an Enumerable#group_by method which does pretty much what you are asking for if I understand correctly. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/