Two ways of achieving duplicate keys in a hash:
h1 = {}
h1.compare_by_identity
h1["a"] = 1
h1["a"] = 2
p h1 # => {"a"=>1, "a"=>2}
h2 = {}
a1 = [1,2,3]
a2 = [1,2]
h2[a1] = 1
h2[a2] = 2
a2 << 3
p h2 # => {[1, 2, 3]=>1, [1, 2, 3]=>2}
More Related Contents:
- Group hashes by keys and sum the values
- How to remove empty string in hash
- Strange, unexpected behavior (disappearing/changing values) when using Hash default value, e.g. Hash.new([])
- Is there any difference between the `:key => “value”` and `key: “value”` hash notations?
- Converting a nested hash into a flat hash
- Is Hash Rocket deprecated?
- Ruby Style: How to check whether a nested hash element exists
- How do I search within an array of hashes by hash values in ruby?
- Performance of Arrays and Hashes in Ruby
- Comparing ruby hashes [duplicate]
- Find key/value pairs deep inside a hash containing an arbitrary number of nested hashes and arrays
- Ruby method Array#
- How to get a specific output iterating a hash in Ruby?
- Accessing elements of nested hashes in ruby [duplicate]
- How do I create a SHA1 hash in ruby?
- Convert array of 2-element arrays into a hash, where duplicate keys append additional values
- How to assign hash[‘a’][‘b’]= ‘c’ if hash[‘a’] doesn’t exist?
- Is order of a Ruby hash literal guaranteed?
- How do methods use hash arguments in Ruby?
- ruby using the “&:methodname” shortcut from array.map(&:methodname) for hash key strings rather than methodname
- How do I compare two hashes?
- Ruby metaclass confusion
- What are all the common ways to read a file in Ruby?
- What does __FILE__ mean in Ruby?
- Ruby: Destructors?
- How to match all occurrences of a regular expression in Ruby
- gem eventmachine fatal error: ‘openssl/ssl.h’ file not found
- Chrome 62 and Flash
- How to determine if a character is a Chinese character
- Should I use alias or alias_method?