有人知道,为什么下面的代码会返回错误:
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