Introducing how to add values when hash keys are duplicated in Ruby.
By changing this
result = result.merge(words)
to this,
result = result.merge(words){|key,v0,v1| v0 + v1}
it will sum (merge) the values of the same key in the two hashes.
Introducing how to add values when hash keys are duplicated in Ruby. By changing `result = result.merge…
Introducing how to add values when hash keys are duplicated in Ruby.
By changing this
result = result.merge(words)
to this,
result = result.merge(words){|key,v0,v1| v0 + v1}
it will sum (merge) the values of the same key in the two hashes.