这段代码:
[{:id => 1,:key => 3},{:id => 2,:key => 4},{:id => 3, :key => 5}].
inject(Hash.new([])){|h,i| h[i[:key]] << i; h}
返回:
{}
虽然这样:
[{:id => 1,:key => 3},{:id => 2,:key => 4},{:id => 3, :key => 5}].
inject(Hash.new([])){|h,i| h[i[:key]] += [i]; h}
返回:
{3=>[{:id=>1, :key=>3}, {:id=>3, :key=>3}], 4=>[{:id=>2, :key=>4}]}
为什么第一个案例的工作方式不同?