How do I put sets or arrays in a two-dimensional Ruby hash?
<p>I want to create a two dimensional hash for holding sets in Ruby. I expect the following code to hold four different sets and also to be able to list the keys in the first dimension of the hash.</p>

<pre><code>require 'set'

cities = Hash.new()
cities['Europe'] = Hash.new(Set.new)
cities['Asia'] = Hash.new(Set.new)

cities['Europe']['Sweden'].add "Stockholm"
cities['Europe']['Sweden'].add "Gothenburg"
cities['Europe']['Denmark'].add "Copenhagen"
cities['Europe']['Denmark'].add "Odense"

cities['Asia']['Japan'].add "Tokyo"
cities['Asia']['Japan'].add "Kyoto"
cities['Asia']['China'].add "Beijing"
cities['Asia']['China'].add "Shanghai"

p cities['Europe']
p cities['Asia']

p cities['Europe']['Sweden']
p cities['Asia']['Japan']
</code></pre>

<p>I get the following output:</p>

<pre><code>{}
{}
#&lt;Set: {"Stockholm", "Gothenburg", "Copenhagen", "Odense"}&gt;
#&lt;Set: {"Tokyo", "Kyoto", "Beijing", "Shanghai"}&gt;
</code></pre>

<p>It seems like the underlying implementation of hashes and sets interfere  with each other and I get this unintuitive result. If instead of sets use arrays (i.e. replace <code>Set.new</code> with <code>Array.new</code> and replace <code>.add</code> with <code>.push</code>) I get the same, unwanted, result. </p>

<p>(Ruby version: ruby 2.3.1p112)</p>