title: Is it good to use fixnums as keys in a Ruby hash?
body: <p>I'm creating a hash to represent a few of the records in a MySQL database. The hash keys corresponds to the database ID fields and the hash values correspond to the database name fields.</p>
  
  <p>What's better &amp; why?</p>
  
  <ol>
  <li><p>Array</p>
  
  <p>This works, but Ruby seems inefficient with sparse arrays because it appears that there's the extra overhead of setting the values of all intermediary indexes tp <code>nil</code>.</p>
  
  <pre><code>fruits = []
  fruits[23] = "apple"
  fruits[109] = "orange"
  # ...
  fruits[23429] = "banana"
  </code></pre></li>
  <li><p>Hash with fixnum as keys</p>
  
  <p>I like this the best, but I've always read it's best to use symbols as keys in a hash. Is it equally as good to use fixnums as keys in a hash? I'm not sure if it is, but I think <code>34.hash</code> because of the nature of fixnums, i.e., <code>34.equal? 34</code> is true whereas <code>"hi".equal? "hi"</code> is false. </p>
  
  <pre><code>fruits = {
    23 =&gt; "apple",
    109 =&gt; "orange",
    # ...
    23429 =&gt; "banana"
  }
  </code></pre></li>
  <li><p>Hash with interned string representations of fixnums as keys</p>
  
  <p>By converting the fixnums to strings and then symbols, I'm able to use symbols as keys. This conversion, however, is annoying, and someone once told me that interning strings is inefficient. Is that so? They just look ugly to me.</p>
  
  <pre><code>fruits = {
    :"23" =&gt; "apple",
    :"109" =&gt; "orange",
    # ...
    :"23429" =&gt; "banana"
  }
  </code></pre></li>
  <li><p>Hash with symbols as keys</p>
  
  <p>I can get prettier symbols (and also use the new Ruby 1.9 hash syntax) by prefixing each key with an alpha character, but then, this solution also requires conversion.</p>
  
  <pre><code>fruits = {
    i23: "apple",
    i109: "orange",
    # ...
    i23429: "banana"
  }
  </code></pre></li>
  </ol>