Hash subtraction in ruby

Viewed 1547

I have 2 hashes (that later will be converted to array, not sure if this is relevant):

Hash1 = {"X"=>2, "Y"=>1, "Z"=>1}

Hash2 = {"X"=>1, "Y"=>1}

I need to subtract them like (Hash3 = Hash1 - Hash2), and I need the result of Hash3 to be, in this case :

Hash3 = {"X"=>1, "Y"=>0, "Z"=>1}

All examples and answers I've seen led to results where the key which had value equals 0 (Y) to be absent in the resulting hash, which is not what I need.

I'm using Ruby 2.3.3 and Rails 5.0

3 Answers
Related