title: Ruby: Creating a hash key and value from a variable in Ruby
body: <p>I have a variable <code>id</code> and I want to use it as a key in a hash so that the value assigned to the variable is used as key of the hash.</p>
  
  <p>For instance, if I have the variable <code>id = 1</code> the desired resulting hash would be <code>{ 1: 'foo' }</code>.</p>
  
  <p>I've tried creating the hash with,</p>
  
  <pre><code>{
    id: 'foo'
  }
  </code></pre>
  
  <p>But that doesn't work, instead resulting in a hash with the symbol <code>:id</code> to <code>'foo'</code>.</p>
  
  <p>I could have sworn I've done this before but I am completely drawing a blank. </p>