From: Robert Klemme Date: 2008-03-25T23:52:29+09:00 Subject: Re: numbers of a record in an array 2008/3/25, Jesús Gabriel y Galán : > On Tue, Mar 25, 2008 at 1:49 PM, Robert Klemme > > wrote: > > 2008/3/25, Jesús Gabriel y Galán : > > > On Tue, Mar 25, 2008 at 11:23 AM, Robert Klemme > > > wrote: > > > > No, this has nothing to do with immediate values. The reason why this > > works is that an operation is used that creates a new instance > > (Fixnum#+ in this case) and uses assignment to place a new value on > > every iteration. This also works with arrays and other non immediate > > and even mutable instances: > > > > irb(main):001:0> collector = Hash.new [] > > => {} > > irb(main):002:0> 10.times {|i| collector[i%3] += [i]} > > => 10 > > > Ah, ok, now I see what you mean. I didn't consider this case, because > you are creating intermediate arrays every iteration. In my head I was > equating the += for Fixnums with << for Arrays in this idiom, if you see > what I mean. That's what I would usually do. But you asked for the reason why the default value setting was sufficient. So I demonstrated with a mutable object and + - could have been anything else (BigDecimal#+ or String#+ for example). Cheers robert -- use.inject do |as, often| as.you_can - without end