Ross Bamford
5/9/2006 9:44:00 AM
On Mon, 08 May 2006 18:31:36 +0100, Robert Klemme <bob.news@gmx.net> wrote:
> Ross Bamford <rosco@roscopeco.remove.co.uk> wrote:
>> Block arguments are handled using regular Ruby assignment rules, so
>> everything from *splat to nested assignment is supported. One place I
>> often find this useful is (contrived example):
>>
>> [[:a,1],[:b,2],[:c,3]].inject({}) { |hsh,(sym, num)| hsh.merge!({ sym
>> => num }) }
>> # => {:b=>2, :a=>1, :c=>3}
>
> I think I now why you use merge! but I'd prefer this one - a tad more
> unelegant but also a tad more efficient:
>
> [[:a,1],[:b,2],[:c,3]].inject({}) { |h,(k, v)| h[k]=v; h}
> => {:b=>2, :c=>3, :a=>1}
>
Yeah, I started throwing merge! in there lately, mostly for that reason
but I guess it probably is quite inefficient really, especially as the
hash grows...
Thanks,
--
Ross Bamford - rosco@roscopeco.remove.co.uk