title: How to access a symbol hash key using a variable in Ruby
body: <p>I have an array of hashes to write a generic checker for, so I want to pass in the name of a key to be checked.  The hash was defined with keys with symbols (colon prefixes). I can't figure out how to use the variable as a key properly. Even though the key exists in the hash, using the variable to access it results in nil.  </p>
  
  <p>In IRB I do this:</p>
  
  <pre><code>&gt;&gt; family = { 'husband' =&gt; "Homer", 'wife' =&gt; "Marge" }
  =&gt; {"husband"=&gt;"Homer", "wife"=&gt;"Marge"}
  &gt;&gt; somevar = "husband"
  =&gt; "husband"
  &gt;&gt; family[somevar]
  =&gt; "Homer"
  &gt;&gt; another_family  = { :husband =&gt; "Fred", :wife =&gt; "Wilma" }
  =&gt; {:husband=&gt;"Fred", :wife=&gt;"Wilma"}
  &gt;&gt; another_family[somevar]
  =&gt; nil
  &gt;&gt;
  </code></pre>
  
  <p>How do I access the hash key through a variable?  Perhaps another way to ask is, how do I coerce the variable to a symbol?</p>