Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are symbols not frozen strings?
    text
    copied!<p>I understand the theoretical difference between Strings and Symbols. I understand that Symbols are meant to represent a concept or a name or an identifier or a label or a key, and Strings are a bag of characters. I understand that Strings are mutable and transient, where Symbols are immutable and permanent. I even like how Symbols <em>look</em> different from Strings in my text editor.</p> <p>What bothers me is that practically speaking, Symbols are so similar to Strings that the fact that they're not <em>implemented</em> as Strings causes a lot of headaches. They don't even support duck-typing or implicit coercion, unlike the other famous "the same but different" couple, Float and Fixnum.</p> <p>The biggest problem, of course, is that hashes coming into Ruby from other places, like JSON and HTTP CGI, use string keys, not symbol keys, so Ruby programs have to bend over backwards to either convert these up front or at lookup time. The mere existence of <code>HashWithIndifferentAccess</code>, and its rampant use in Rails and other frameworks, demonstrates that there's a problem here, an itch that needs to be scratched.</p> <p>Can anyone tell me a practical reason why Symbols should not be frozen Strings? Other than "because that's how it's always been done" (historical) or "because symbols are not strings" (begging the question).</p> <p>Consider the following astonishing behavior:</p> <pre><code>:apple == "apple" #=&gt; false, should be true :apple.hash == "apple".hash #=&gt; false, should be true {apples: 10}["apples"] #=&gt; nil, should be 10 {"apples" =&gt; 10}[:apples] #=&gt; nil, should be 10 :apple.object_id == "apple".object_id #=&gt; false, but that's actually fine </code></pre> <p>All it would take to make the next generation of Rubyists less confused is this:</p> <pre><code>class Symbol &lt; String def initialize *args super self.freeze end </code></pre> <p>(and a lot of other library-level hacking, but still, not too complicated)</p> <p>See also:</p> <ul> <li><a href="http://onestepback.org/index.cgi/Tech/Ruby/SymbolsAreNotImmutableStrings.red" rel="nofollow noreferrer">http://onestepback.org/index.cgi/Tech/Ruby/SymbolsAreNotImmutableStrings.red</a></li> <li><a href="http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol" rel="nofollow noreferrer">http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol</a></li> <li><a href="https://stackoverflow.com/questions/10098935/why-does-my-code-break-when-using-a-hash-symbol-instead-of-a-hash-string">Why does my code break when using a hash symbol, instead of a hash string?</a></li> <li><a href="https://stackoverflow.com/questions/8189416/why-use-symbols-as-hash-keys-in-ruby">Why use symbols as hash keys in Ruby?</a></li> <li><a href="https://stackoverflow.com/questions/6745010/what-are-symbols-and-how-do-we-use-them">What are symbols and how do we use them?</a></li> <li><a href="https://stackoverflow.com/questions/9712706/ruby-symbols-vs-strings-in-hashes">Ruby Symbols vs Strings in Hashes</a></li> <li><a href="https://stackoverflow.com/questions/6725518/cant-get-the-hang-of-symbols-in-ruby">Can&#39;t get the hang of symbols in Ruby</a></li> <li><a href="http://blog.arkency.com/could-we-drop-symbols-from-ruby/" rel="nofollow noreferrer">http://blog.arkency.com/could-we-drop-symbols-from-ruby/</a></li> <li><a href="https://stackoverflow.com/questions/44634073/do-ruby-symbols-exist-because-strings-are-mutable-and-not-interned/44635947">Do Ruby symbols exist because strings are mutable and not interned?</a></li> </ul> <p>Update: I think Matz makes the case for <code>class Symbol &lt; String</code> very well here: <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/9192" rel="nofollow noreferrer">http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/9192</a> (thanks to Azolo for digging this up, and also Matz' eventual retraction).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload