[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Loop weirdness

Kroeger, Simon (ext)

1/31/2006 1:05:00 PM


> > 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?

that's the way multiple assignement works in ruby

a, b = 1, 2

a, b = [1, 2]

is equivalent (at least for this purpose).

map yields arrays of two elements in this case and I'm assigning them to
two variables.

cheers

Simon