Ruby - Hash with Duplicate Key

Introduction

If you use the same key twice in a hash, you will end up overwriting the original value.

Consider this example:

h2['Product1'] = 'A' 
h2['Product2'] = 'B' 
h2['Product3'] = 'C' 
h2['Product1'] = 'D' 

Here the key 'Product1' has been used twice.

As a consequence, the original value, 'A', has been replaced by 'D', resulting in this hash:

Related Topic