From: Matthew Moss Date: 2006-03-13T12:59:02+09:00 Subject: Re: Array#inject with hash as initial, unexpected error Okay, I feel silly now. Thanks for the reminder on basic assignment protocol. =) On 3/12/06, Trevor Squires wrote: > Hey Matthew, > > observe: > > irb(main):001:0> [] << 100 > => [100] > irb(main):002:0> {}[:something] = 100 > => 100 > > The << operator on an array returns the array, while the [] operator > on a hash returns the value you assigned. > > And the result of the last statement in your inject block will > replace the value for your memo on each iteration. > > So... just make sure the last statement in your inject block is the > memo value you are accumulating: > > [1,2,3].inject({}) { |memo, val| memo[val] = val**2; memo}