次のコードがエラーを返す理由は誰にも分かりますか?
stock = {"M9788375085969"=>5, "M9788392289760"=>5, "M9788389371461"=>1, "M9788389371447"=>3, "M9788392289761"=>2}
add = {"M9788375085969"=>1, "M9788392289760"=>2, "NEW9788392289753"=>1 }
add.each do |key, value|
stock[key] += value
end
NoMethodError: undefined method `+' for nil:NilClass
同様のことがうまくいきますが:
key = "M9788375085969"
value = 1
stock[key] += value
=> 6