array1 = { "d1" => 2, "d2" => 3}
array2 = { "d1" => 3, "d3" => 10}
我想這一點:
array3 = { "d1" => 5, "d2" => 3, "d3" => 10}
加起來值哈希
我想這一點,這是行不通的。我得到的錯誤:「NoMethodError:未定義的方法`+」的零:NilClass」
array3 = {}
array1.each {|key, count| array3[key] += count}
array2.each {|key, count| array3[key] += count}
如果您關注的是簡潔,你可以改變這:`array3 = array1.merge(array2){| _,v1,v2 | v1 + v2}` – harbichidian 2011-02-16 02:53:14