From: Jonathan Leighton Date: 2006-01-31T20:08:58+09:00 Subject: Re: Loop weirdness On Tue, 2006-01-31 at 06:27 +0900, Simon Kræ—¦ger wrote: > > > > I did, back in the thread a little: > > Sorry, missed that. > > > But don't worry about it, I am happy with the solution I've found. (That > > is, there is no longer a *problem*, although I'm always interested to > > see better/leaner/cleaner ways of doing things). > > :) another try: > > def categorise(items) > items.inject([]) do |a, item| > ary = a.assoc(item[:num]) || (a << [item[:num], []]).last > ary.last << item[:foo]; a > end.map{|k, v| {:num => k, :items => v}} > end That's pretty slick :). One question though: The docs say Array#map only yeilds on value: the value of the item in the array. However, you are using two in your block. I understand (from looking at the code), that this assigns each value in the item array to k and v respectively, but how does that work? Is there any docs about that anywhere? Cheers -- Jonathan Leighton http://turnipspatch.com/ | http://jonathanleighton.com/ | http://digital-proof.org/