title: How to understand symbols in Ruby
body: <p>Despite reading "<a href="http://web.archive.org/web/20140314193235/http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols" rel="noreferrer">Understanding Ruby Symbols</a>", I'm still confused by the representation of the data in memory when it comes to using symbols.  If a symbol, two of them contained in different objects, exist in the same memory location, then how is it that they contain <em>different</em> values?  I'd have expected the same memory location to contain the same value.</p>
  
  <p>This a quote from the link:</p>
  
  <blockquote>
    <p>Unlike strings, symbols of the same name are initialized and exist in memory only once during a session of ruby</p>
  </blockquote>
  
  <p>I don't understand how it manages to differentiate the values contained in the same memory location.</p>
  
  <p>Consider this example:</p>
  
  <pre><code>patient1 = { :ruby =&gt; "red" }
  patient2 = { :ruby =&gt; "programming" }
  
  patient1.each_key {|key| puts key.object_id.to_s}
  3918094
  patient2.each_key {|key| puts key.object_id.to_s}
  3918094
  </code></pre>
  
  <p><code>patient1</code> and <code>patient2</code> are both hashes, that's fine.  <code>:ruby</code> however is a symbol.  If we were to output the following:</p>
  
  <pre><code>patient1.each_key {|key| puts key.to_s}
  </code></pre>
  
  <p>Then what will be output? <code>"red"</code>, or <code>"programming"</code>?</p>
  
  <p>Forgetting hashes for a second, I'm thinking a symbol is a <em>pointer</em> to a value.  The questions I have are:</p>
  
  <ul>
  <li>Can I assign a value to a symbol?</li>
  <li>Is a symbol just a pointer to a variable with a value in it?</li>
  <li>If symbols are global, does that mean a symbol always points to one thing?</li>
  </ul>