<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Random Hacks: 13 Ways of Looking at a Ruby Symbol</title>
    <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Technology and Other Fun Stuff</description>
    <item>
      <title>"13 Ways of Looking at a Ruby Symbol" by Eric Kidd</title>
      <description>&lt;p&gt;Over the years, quite a few people have asked me, &amp;#8220;What is symbol?&amp;#8221; I usually tell them, &amp;#8220;A symbol is a name.&amp;#8221; And for some people, this clicks instantly.&lt;/p&gt;


	&lt;p&gt;Other people quite sensibly reply, &amp;#8220;Well, why is there one kind of strings for &amp;#8216;names,&amp;#8217; and another kind of strings for, well, strings? How do I decide when to use which?&amp;#8221;&lt;/p&gt;


	&lt;p&gt;And those aren&amp;#8217;t such easy questions to answer. Rails confuses the question further by making symbols and strings pretty much interchangeable.&lt;/p&gt;


	&lt;p&gt;What I&amp;#8217;ve found is that different explanations work for different people. One programmer said, &amp;#8220;Oh! They&amp;#8217;re just enumeration labels! That makes sense.&amp;#8221; And when I was a novice Lisp programmer, #10 was the one that made things click for me.&lt;/p&gt;


	&lt;p&gt;So please, take whatever explanation works for you, and ignore the rest. And if you&amp;#8217;d prefer a simpler explanation, one which only focuses on a single aspect of symbols, please feel free to link to it from the comment thread!&lt;/p&gt;</description>
      <pubDate>Thu, 08 Feb 2007 07:37:10 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:5f09576d-608f-4963-bb29-8f12a3d32c53</guid>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#comment-277</link>
    </item>
    <item>
      <title>"13 Ways of Looking at a Ruby Symbol" by Charles Oliver Nutter</title>
      <description>&lt;p&gt;Austin: I think you must have meant #13 is not true for JRuby, because obviously #12 must be. Just wanted to clarify that for anyone reading&amp;#8230;&lt;/p&gt;


	&lt;p&gt;And I agree that Symbols are names. That&amp;#8217;s how I describe them, and it&amp;#8217;s a core reason I argued against making them &lt; String.&lt;/p&gt;</description>
      <pubDate>Thu, 08 Feb 2007 02:53:36 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:14af9247-93a8-43cb-ae91-0920b3457a95</guid>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#comment-276</link>
    </item>
    <item>
      <title>"13 Ways of Looking at a Ruby Symbol" by Austin Ziegler</title>
      <description>&lt;p&gt;I can&amp;#8217;t disagree more, Michael. The reality is that the more attention that is paid to what a Symbol &lt;strong&gt;is&lt;/strong&gt;, the less attention is paid to the &lt;strong&gt;intent&lt;/strong&gt;. Symbols aren&amp;#8217;t magic. They&amp;#8217;re not complex. Talking about how they&amp;#8217;re implemented is, for most people and &lt;strong&gt;especially&lt;/strong&gt; Rails folks, unnecessary and confusing.&lt;/p&gt;


	&lt;p&gt;Symbols are names. That&amp;#8217;s all that matters. Saying anything &lt;strong&gt;more&lt;/strong&gt; than that while trying to &lt;strong&gt;define&lt;/strong&gt; a Symbol is a fruitless exercise. What&amp;#8217;s in a name? The meaning one gives to it. You could call me &amp;#8220;Betty&amp;#8221;, but that doesn&amp;#8217;t mean that I&amp;#8217;d respond to it&amp;#8212;it doesn&amp;#8217;t have meaning to me.&lt;/p&gt;


	&lt;p&gt;Ten of the ways (1, 2, 5 &amp;#8211; 10, 12) are simply ways of saying exactly what #3 says, and they&amp;#8217;re less useful because they try to make comparisons that deal with &lt;span class="caps"&gt;HOW &lt;/span&gt;Symbols are used, not &lt;span class="caps"&gt;WHAT&lt;/span&gt; they are. #11 is important to know about dynamically generating symbols (an implication, if you will), and #12 matters &lt;span class="caps"&gt;ONLY&lt;/span&gt; to people who are diving under the covers of Ruby into its implementation. (It&amp;#8217;s not true in any case, for JRuby, which is a damned good argument for not including it.)&lt;/p&gt;</description>
      <pubDate>Wed, 07 Feb 2007 23:58:18 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:bdd1149d-1ef1-4986-bd73-232348d5eb70</guid>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#comment-275</link>
    </item>
    <item>
      <title>"13 Ways of Looking at a Ruby Symbol" by Michael</title>
      <description>&lt;p&gt;Actually, Austin, I think Eric&amp;#8217;s post goes a long way to &lt;em&gt;removing&lt;/em&gt; a sense of complexity and &amp;ldquo;magic&amp;rdquo; from some people&amp;#8217;s interpretation of the symbol as a data structure, and I would argue he provides some very helpful perspectives.  To some, these views may seem obvious; but everything is obvious once you&amp;#8217;ve seen it.&lt;/p&gt;


	&lt;p&gt;As with many of the basic features of any programming language, symbols are a simple idea that gains a kind of complexity by virtue of its generality.  Therefore, I think there is real value in exploring many different views of the same simple concept, even though doing so does not change its intrinsic simplicity.&lt;/p&gt;</description>
      <pubDate>Tue, 06 Feb 2007 23:28:46 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:dba2dcc8-3383-4cf9-95e5-4625179d1af0</guid>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#comment-273</link>
    </item>
    <item>
      <title>"13 Ways of Looking at a Ruby Symbol" by Austin Ziegler</title>
      <description>&lt;p&gt;Um. I think that this makes post makes Symbol objects far more complex and magical than they really are. They&amp;#8217;re not magic. There&amp;#8217;s a few interesting facts about them (there&amp;#8217;s only one copy of a given symbol; they&amp;#8217;re not currently garbage-collectable; how they&amp;#8217;re implemented in C), but nothing else really helps people understand Symbols.&lt;/p&gt;


	&lt;p&gt;I said it in the O&amp;#8217;Reilly blog &lt;a href="http://www.oreillynet.com/ruby/blog/2005/12/symbols_strings_methods_and_va.html"&gt;months ago&lt;/a&gt;, there&amp;#8217;s nothing magical or special about Symbols. It&amp;#8217;s in the &lt;strong&gt;intent&lt;/strong&gt; of the call you&amp;#8217;re making. What&amp;#8217;s magic in &amp;#8220;attr_accessor :foo&amp;#8221; is not :foo, but &amp;#8220;attr_accessor&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;Anonymous Cow, matz has backed off from that proposal, if I remember correctly.&lt;/p&gt;</description>
      <pubDate>Tue, 06 Feb 2007 22:40:43 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:4d449acd-9c4a-4cb3-b9a7-79cdfd640076</guid>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#comment-272</link>
    </item>
    <item>
      <title>"13 Ways of Looking at a Ruby Symbol" by Anonymous Cow</title>
      <description>&lt;p&gt;But I hear tell that in 2.0, Symbols &lt;strong&gt;will&lt;/strong&gt; be strings, oh my!&lt;/p&gt;


	&lt;p&gt;(Though, they will be thoroughly frozen, as to not harm the children of the world).&lt;/p&gt;</description>
      <pubDate>Mon, 05 Feb 2007 21:39:39 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:34c11115-6dbf-4645-940d-08509b54005b</guid>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#comment-271</link>
    </item>
    <item>
      <title>"13 Ways of Looking at a Ruby Symbol" by Eric Kidd</title>
      <description>&lt;p&gt;Murray: Yes, the code in number 7 is extremely useless. :-)&lt;/p&gt;</description>
      <pubDate>Mon, 05 Feb 2007 19:57:11 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:63077e70-3465-4cd7-a9bd-9fe568a7fbe8</guid>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#comment-270</link>
    </item>
    <item>
      <title>"13 Ways of Looking at a Ruby Symbol" by Murray Spork</title>
      <description>&lt;p&gt;2nd array element is (now correctly escaped): &lt;br /&gt;
#&amp;lt;Method: Demo#say&amp;gt;&lt;/p&gt;</description>
      <pubDate>Mon, 05 Feb 2007 19:43:48 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:606cf662-0bf7-4608-9b37-90b021d925bf</guid>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#comment-269</link>
    </item>
    <item>
      <title>"13 Ways of Looking at a Ruby Symbol" by Murray Spork</title>
      <description>&lt;p&gt;Regarding no.7 &amp;#8211; not sure if I got the point entirely &amp;#8211; but for the 2nd item in the arrary returned by look_up_with_symbols  be:&lt;/p&gt;


	&lt;p&gt;method(:say).call&lt;/p&gt;


	&lt;p&gt;Otherwise you get the following array returned:&lt;/p&gt;


	&lt;p&gt;=&gt; [&amp;#8220;Hello&amp;#8221;, #&lt;Method: Demo#say&gt;, &amp;#8220;Hello&amp;#8221;]&lt;/p&gt;</description>
      <pubDate>Mon, 05 Feb 2007 19:27:12 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:39125ad0-0ec0-40f8-a097-c45970d0ef0f</guid>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#comment-268</link>
    </item>
    <item>
      <title>"13 Ways of Looking at a Ruby Symbol" by Eric Kidd</title>
      <description>&lt;p&gt;Thanks for the suggestion! Here&amp;#8217;s a sample function with keyword arguments in Ruby:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;foo&lt;/span&gt; &lt;span class="ident"&gt;opts&lt;/span&gt;&lt;span class="punct"&gt;={}&lt;/span&gt;
  &lt;span class="ident"&gt;bar&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;opts&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="symbol"&gt;:bar&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;||&lt;/span&gt; &lt;span class="number"&gt;1&lt;/span&gt;
  &lt;span class="ident"&gt;baz&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;opts&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="symbol"&gt;:baz&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;||&lt;/span&gt; &lt;span class="number"&gt;2&lt;/span&gt;
  &lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="ident"&gt;baz&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="ident"&gt;foo&lt;/span&gt; &lt;span class="symbol"&gt;:baz&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="number"&gt;3&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This is a little clunky, but it works nicely enough in practice.&lt;/p&gt;</description>
      <pubDate>Fri, 02 Feb 2007 19:39:12 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:56a77429-2436-4e41-8ab6-5a5c429e7634</guid>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#comment-258</link>
    </item>
    <item>
      <title>"13 Ways of Looking at a Ruby Symbol" by Peter Burns</title>
      <description>&lt;p&gt;You might want to go into more detail about using symbols for keyword arguments.  That&amp;#8217;s just using a convenient way that Ruby deals with hashes at the end of argument lists.&lt;/p&gt;


	&lt;p&gt;For example: http://api.rubyonrails.org/classes/ActionController/Base.html#M000261&lt;/p&gt;


	&lt;p&gt;The way you have it seems misleading to someone new to Ruby, suggesting that&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;def foo(bar=1,baz=2)
  puts baz
end&lt;/code&gt;&lt;/pre&gt;


	&lt;pre&gt;&lt;code&gt;foo :baz =&amp;gt; 3 #=&amp;gt; prints:   2&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;would output 3.  Rails is just using an options hash at the end of parameter lists to do something like named arguments.&lt;/p&gt;</description>
      <pubDate>Fri, 02 Feb 2007 18:30:12 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:468e3d3f-b5e9-4aa4-af9b-2a1784d5272a</guid>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#comment-257</link>
    </item>
    <item>
      <title>"13 Ways of Looking at a Ruby Symbol" by Eric Kidd</title>
      <description>&lt;p&gt;Thanks! Fixed it.&lt;/p&gt;</description>
      <pubDate>Fri, 02 Feb 2007 12:58:53 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:2be26595-47c9-4cf0-96fa-c7d1c964e512</guid>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#comment-254</link>
    </item>
    <item>
      <title>"13 Ways of Looking at a Ruby Symbol" by Michael Chermside</title>
      <description>&lt;p&gt;&amp;#8220;In Ruby, we can convert a symbol to a string using intern:&amp;#8221; should read &amp;#8220;In Ruby, we can convert a string to a symbol using intern:&amp;#8221; instead.&lt;/p&gt;</description>
      <pubDate>Fri, 02 Feb 2007 11:24:04 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:ccb02b81-ad58-4d4d-b5a4-989bc63dd76d</guid>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#comment-253</link>
    </item>
    <item>
      <title>13 Ways of Looking at a Ruby Symbol</title>
      <description>&lt;p&gt;New Ruby programmers often ask, &amp;#8220;What, exactly, &lt;em&gt;is&lt;/em&gt; a symbol? And how does
it differ from a string?&amp;#8221;  No one answer works for everybody, so&amp;#8211;with
&lt;a href="http://www.poets.org/viewmedia.php/prmMID/15746"&gt;apologies to Wallace Stevens&lt;/a&gt;&amp;#8211;here are 13 ways of looking at a
Ruby symbol.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol"&gt;Read More&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 20 Jan 2007 03:20:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:46f3fe2b-e387-46d2-a682-8047b0fbf523</guid>
      <author>Eric Kidd</author>
      <link>http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol</link>
      <category>Ruby</category>
      <trackback:ping>http://www.randomhacks.net/articles/trackback/194</trackback:ping>
    </item>
  </channel>
</rss>
