Quick way to split up an array of objects into groups?

I have a collection of objects, which i want to separate into groups
based on a particular property (to be specific, two attributes - that is
if obj.foo and obj.bar are both the same for two objects they go into an
array together). Not every object has a duplicate, but most of them do.
So, i'd end up with an array, where each member is another array, with
the subarrays holding either 1 or 2 (or maybe more) objects that match
on foo and bar.

Can anyone tell me a nice and simple way of doing this?

thanks
max

···

--
Posted via http://www.ruby-forum.com/.

Enumerable#parition or Set#classify might help.

-- fxn

···

On Jan 21, 2008, at 12:23 PM, Max Williams wrote:

I have a collection of objects, which i want to separate into groups
based on a particular property (to be specific, two attributes - that is
if obj.foo and obj.bar are both the same for two objects they go into an
array together). Not every object has a duplicate, but most of them do.
So, i'd end up with an array, where each member is another array, with
the subarrays holding either 1 or 2 (or maybe more) objects that match
on foo and bar.

Can anyone tell me a nice and simple way of doing this?

Xavier Noria 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

···

On Jan 21, 2008, at 12:23 PM, Max Williams wrote:

--
Posted via http://www.ruby-forum.com/\.

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.

···

On Jan 21, 2008 8:07 AM, Max Williams <toastkid.williams@gmail.com> 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

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Xavier Noria 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]

Note there is an assignment instead of an equals operator there.

end

Yes, I think that's equivalent to

   collection.to_set.classify {|x| [x.foo, x.bar]}.values

plus perhaps a map to_a.

-- fxn

···

On Jan 21, 2008, at 2:07 PM, Max Williams wrote:

On Jan 21, 2008, at 12:23 PM, Max Williams wrote:

Rick Denatale wrote:

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/

Ah right, i see now :slight_smile: cheers!

···

--
Posted via http://www.ruby-forum.com/\.

Xavier Noria wrote:

Note there is an assignment instead of an equals operator there.

end

Yes, I think that's equivalent to

   collection.to_set.classify {|x| [x.foo, x.bar]}.values

plus perhaps a map to_a.

-- fxn

That's not really my code, it's a paraphrased version for clarity :slight_smile:
Cheers for the tip about to_set.classify though.

···

--
Posted via http://www.ruby-forum.com/\.