# What is the difference between :foo and "foo"?

**URL:** <https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653>\
**Category:** ruby-talk\
**Created:** [28 December 2005 19:27 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653 "2005-12-28T19:27:53Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [28 December 2005 20:51 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/21 "2005-12-28T20:51:03Z")

</div>

> Oh, I get it!!!  
> In see itwould be some\_call(&@my\_variable), and in ruby it's  
> some\_call(:my\_variable). One thing -- why not some\_call(:@my\_variable)?

No.

There is nothing even remotely close in C, C++, or Java.

In Ruby, it's \*not\* some\_call(:my\_variable), it's some\_call(:my\_name).

When you do:

&nbsp;&nbsp;&nbsp;attr\_accessor :my\_name

You do NOT get a @my\_name variable. You get two methods: Foo#my\_name  
and Foo#my\_name= -- that's it. Consider:

> > class Foo  
> > &nbsp;&nbsp;attr\_accessor :bar  
> > end

=\> nil

> > baz = Foo.new

=\> #\<Foo:0x2d8aea8\>

Note. Thus far, there's no instance variable @bar on the Foo instance baz.

> > Foo.instance\_methods(false)

=\> ["bar", "bar="]

There's our instance methods.

> > baz.bar = 32

=\> 32

> > baz

=\> #\<Foo:0x2d8aea8 @bar=32\>

Now that we've called Foo#bar= on the baz instance of Foo class, baz  
finally has a @bar instance variable. But not a moment before, unless  
we instantiate such an instance variable prior to the call of  
Foo#bar=.

So :bar is a name (Symbol) used to refer to the name :bar. It is used  
by attr\_accessor to create two methods that also operate on a  
like-named instance variable. But :bar doesn't refer to a variable,  
which is precisely why it isn't :@bar -- you're not creating a  
variable @bar, you're creating instance methods #bar and #bar= that  
happen to work on @bar in the instance.

-austin

> **···**
>
> On 28/12/05, Steve Litt \<slitt@earthlink.net\> wrote:  
> --  
> Austin Ziegler \* halostatue@gmail.com  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* Alternate: austin@halostatue.ca

---

<div class="post-metadata">

**Author:** ![Johannes\_Friestad](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Johannes\_Friestad](https://rubytalk.org/u/Johannes_Friestad)\
**Post date:** [28 December 2005 21:38 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/22 "2005-12-28T21:38:27Z")

</div>

That's where the 'can be' part comes in 🙂  
The point is that symbols support quicker lookup by their nature.  
Whether they are quicker in practice will depend on the  
implementation. From the timings you give, it looks like symbol lookup  
is implemented by converting the symbol to a string and doing string  
lookup. Which is obviously not quicker 🙂

My data were from a Common Lisp implementation, where symbols were  
quicker in practice as well.  
Sorry, didn't know about the Ruby implementation. Thanks for the info.

jf

> **···**
>
> On 12/28/05, ara.t.howard@noaa.gov \<ara.t.howard@noaa.gov\> wrote:
> 
> > On Thu, 29 Dec 2005, Johannes Friestad wrote:
> > 
> > \> Symbols are (or can be) quicker for hash lookup, since it is sufficient to  
> > \> compare object identity to find whether two symbols are the same, while  
> > \> strings must be compared character by character. You are unlikely to notice  
> > \> the difference unless your program uses hashes heavily.
> > 
> > i see this claim all the time but never data supporting it, all my test  
> > programs have shown the opposite to be true. see

---

<div class="post-metadata">

**Author:** ![Jim\_Weirich1](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jim\_Weirich1](https://rubytalk.org/u/Jim_Weirich1)\
**Post date:** [28 December 2005 22:12 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/23 "2005-12-28T22:12:57Z")

</div>

ara wrote:

> i see this claim all the time but never data supporting it, all my test  
> programs have shown the opposite to be true.

My benchmark shows symbols to have a slight edge. I've attached my  
benchmark at the end for review. (Benchmarks are tricky ... sometimes  
you don't measure what you think you are measuring).

&nbsp;&nbsp;&nbsp;&nbsp;$ ruby string\_symbol\_time.rb  
&nbsp;&nbsp;&nbsp;&nbsp;Strings Filling  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;14.080000 0.090000 14.170000 ( 14.406058)  
&nbsp;&nbsp;&nbsp;&nbsp;Strings Fetching  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.320000 0.030000 4.350000 ( 4.355025)

&nbsp;&nbsp;&nbsp;&nbsp;Symbols Filling  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12.300000 0.030000 12.330000 ( 12.561648)  
&nbsp;&nbsp;&nbsp;&nbsp;Symbols Fetching  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3.370000 0.030000 3.400000 ( 3.461109)

> also, don't forget that symbols are \_\_never\_\_ freed.

True, but when used properly, this is rarely a concern. If they are  
used as programmer names for things, then the number of symbols is  
finite and not likely to grow and consume memory as the program runs.

If however, you are dynamically creating symbols by interning strings, I  
would suggest you review your use of symbols and consider using strings  
instead.

> **···**
>
> --  
> -- Jim Weirich
> 
> -------------------------------------------------------------------  
> #!/usr/bin/env ruby
> 
> require 'benchmark'
> 
> SIZE = 100  
> N = 10000
> 
> def make\_str\_keys  
> &nbsp;&nbsp;(1..SIZE).collect { |i| "key#{i}" }  
> end
> 
> def make\_sym\_keys(strs)  
> &nbsp;&nbsp;strs.collect { |s| s.intern }  
> end
> 
> def populate(keys)  
> &nbsp;&nbsp;result = {}  
> &nbsp;&nbsp;keys.each\_with\_index do |k, i|  
> &nbsp;&nbsp;&nbsp;&nbsp;result[k] = i  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;result  
> end
> 
> def fetch(keys, hash)  
> &nbsp;&nbsp;keys.each do |key| hash[key] end  
> end
> 
> strs = make\_str\_keys  
> syms = make\_sym\_keys(strs)
> 
> str\_hash = populate(strs)  
> sym\_hash = populate(syms)
> 
> puts "Strings Filling"  
> puts Benchmark.measure {  
> &nbsp;&nbsp;N.times do  
> &nbsp;&nbsp;&nbsp;&nbsp;populate(strs)  
> &nbsp;&nbsp;end  
> }
> 
> puts "Strings Fetching"  
> puts Benchmark.measure {  
> &nbsp;&nbsp;N.times do  
> &nbsp;&nbsp;&nbsp;&nbsp;fetch(strs, str\_hash)  
> &nbsp;&nbsp;end  
> }
> 
> puts  
> puts "Symbols Filling"  
> puts Benchmark.measure {  
> &nbsp;&nbsp;N.times do  
> &nbsp;&nbsp;&nbsp;&nbsp;populate(syms)  
> &nbsp;&nbsp;end  
> }
> 
> puts "Symbols Fetching"  
> puts Benchmark.measure {  
> &nbsp;&nbsp;N.times do  
> &nbsp;&nbsp;&nbsp;&nbsp;fetch(syms, sym\_hash)  
> &nbsp;&nbsp;end  
> }
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Dan6](https://avatars.discourse-cdn.com/v4/letter/d/d2c977/32.png) [@Dan6](https://rubytalk.org/u/Dan6)\
**Post date:** [28 December 2005 22:18 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/24 "2005-12-28T22:18:54Z")

</div>

How about devoting the next Ruby Quiz to coming up with the best-of-class examples, self paced-tutorial and documentation to settle the :symbol vs "string" issue? At some point you have to ask yourself are the explanations given so far to inquiring users adequate. The fact that this question keeps coming up must be seen as evidence that there is something lacking in the explanations previously given. At a minimum all the explanations give so far should be edited up into a FAQ entry that the experts can agree upon. Just my two cents.  
&nbsp;&nbsp;&nbsp;&nbsp;6.1 What does :var mean? A colon followed by a name generates an integer(Fixnum) called a symbol which corresponds one to one with the identifier. "var".intern gives the same integer as :var, but the ``:'' form will create a local symbol if it doesn't already exist. The routines "catch", "throw", "autoload", and so on, require a string or a symbol as an argument. "method\_missing", "method\_added" and "singleton\_method\_added" (and others) require a symbol. The fact that a symbol springs into existence the first time it is referenced is sometimes used to assign unique values to constants: NORTH = :NORTH  
SOUTH = :SOUTH  
EAST = :EAST  
WEST = :WEST

&nbsp;&nbsp;[http://www.rubycentral.com/faq/rubyfaqall.html#s6](http://www.rubycentral.com/faq/rubyfaqall.html#s6)

> **···**
>
> ---------------------------------  
> Yahoo! for Good - Make a difference this year.

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [28 December 2005 23:33 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/25 "2005-12-28T23:33:27Z")

</div>

Johannes Friestad wrote:  
...

> HOST = :host  
> PORT = :port  
> foo1 = {  
> &nbsp;&nbsp;&nbsp;&nbsp;HOST =\> 'localhost',  
> &nbsp;&nbsp;&nbsp;&nbsp;PORT =\> 80  
> }
> 
> which is equally safe, simpler to read, doesn't need the ConstUtil,  
> and automagically gives a constant with a readable string value.

My hack was a quicky to flesh out the example. After sending it I thought about assigning symbols. My main point was that if one mistypes a symbol name, Ruby doesn't care. Unit tests should catch this, but using constants might just help things along because of the immediate error. And it might more clearly express intent.

James

> **···**
>
> --
> 
> [http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
> [Ruby Code & Style](http://www.artima.com/rubycs/) - Ruby Code & Style: Writers wanted  
> [http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
> [http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
> [http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools

---

<div class="post-metadata">

**Author:** ![Johannes\_Friestad](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Johannes\_Friestad](https://rubytalk.org/u/Johannes_Friestad)\
**Post date:** [29 December 2005 02:25 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/26 "2005-12-29T02:25:51Z")

</div>

Along with Jim and Mauricio, my tests indicate that symbols are  
consistently quicker, even on short strings.

Here's my benchmark

> **···**
>
> On 12/28/05, ara.t.howard@noaa.gov \<ara.t.howard@noaa.gov\> wrote:
> 
> > On Thu, 29 Dec 2005, Johannes Friestad wrote:
> > 
> > \> Symbols are (or can be) quicker for hash lookup, since it is sufficient to  
> > \> compare object identity to find whether two symbols are the same, while  
> > \> strings must be compared character by character. You are unlikely to notice  
> > \> the difference unless your program uses hashes heavily.
> > 
> > i see this claim all the time but never data supporting it, all my test  
> > programs have shown the opposite to be true.
> 
> -------  
> &nbsp;&nbsp;def bmark\_string\_symb  
> &nbsp;&nbsp;&nbsp;&nbsp;require 'benchmark'  
> &nbsp;&nbsp;&nbsp;&nbsp;strings, symbols=,   
> &nbsp;&nbsp;&nbsp;&nbsp;n, m=100, 1000  
> &nbsp;&nbsp;&nbsp;&nbsp;hash={}  
> &nbsp;&nbsp;&nbsp;&nbsp;n.times {|x| strings\<\<strings\<\<x.to\_s+"key"}  
> &nbsp;&nbsp;&nbsp;&nbsp;strings.each {|s| symbols\<\<s.to\_sym}  
> &nbsp;&nbsp;&nbsp;&nbsp;# initialize hash  
> &nbsp;&nbsp;&nbsp;&nbsp;strings.each {|s| hash[s]=1}  
> &nbsp;&nbsp;&nbsp;&nbsp;symbols.each {|s| hash[s]=1}  
> &nbsp;&nbsp;&nbsp;&nbsp;Benchmark.bm(10) do |b|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.report("string set") { m.times {|x| strings.each {|s| hash[s]=x}}}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.report("symbol set") { m.times {|x| symbols.each {|s| hash[s]=x}}}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.report("string get") { m.times {|x| strings.each {|s| hash[s]}}}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b.report("symbol get") { m.times {|x| symbols.each {|s| hash[s]}}}  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> -------
> 
> and here are some results:  
> -------  
> irb(main):080:0\> bmark\_string\_symb  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> string set 0.219000 0.016000 0.235000 ( 0.235000)  
> symbol set 0.141000 0.000000 0.141000 ( 0.141000)  
> string get 0.078000 0.000000 0.078000 ( 0.078000)  
> symbol get 0.047000 0.000000 0.047000 ( 0.047000)  
> =\> true  
> =\> true  
> irb(main):083:0\> bmark\_string\_symb  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> string set 0.234000 0.000000 0.234000 ( 0.235000)  
> symbol set 0.063000 0.000000 0.063000 ( 0.062000)  
> string get 0.078000 0.000000 0.078000 ( 0.078000)  
> symbol get 0.047000 0.000000 0.047000 ( 0.047000)  
> =\> true  
> -------
> 
> There's a fair amount of variation, but symbols appear to behave as  
> expected (quicker on average), meaning that my guess that symbol  
> lookup in hashes was done on the basis of their string value was  
> wrong.  
> I guess I should learn to refrain from speculating until I've checked closer 🙂
> 
> jf

---

<div class="post-metadata">

**Author:** ![Ron\_M](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@Ron\_M](https://rubytalk.org/u/Ron_M)\
**Post date:** [29 December 2005 10:43 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/27 "2005-12-29T10:43:56Z")

</div>

I still wonder if the lack of garbage collection of symbols  
is merely a weakness in the current implementation, or something  
that's part of the Ruby language.

Note that most lisps and javas can garbage collect  
symbols and interned strings.

If it's just an implementation misfeature I'd love to  
see that limitation go away in YARV.

Notes:  
\*1 [http://community.schemewiki.org/?scheme-faq-language](http://community.schemewiki.org/?scheme-faq-language) "Most Schemes  
&nbsp;&nbsp;&nbsp;&nbsp;do perform garbage-collection of symbols, since otherwise programs  
&nbsp;&nbsp;&nbsp;&nbsp;using string-\>symbol to dynamically create symbols would consume  
&nbsp;&nbsp;&nbsp;&nbsp;ever increasing amounts of memory even if the created symbols are  
&nbsp;&nbsp;&nbsp;&nbsp;no longer being used."

\*2 [interned Strings : Java Glossary](http://mindprod.com/jgloss/interned.html) "In the early JDKs, any  
&nbsp;&nbsp;&nbsp;&nbsp;string you interned could never be garbage collected because the  
&nbsp;&nbsp;&nbsp;&nbsp;JVM had to keep a reference to in its Hashtable so it could  
&nbsp;&nbsp;&nbsp;&nbsp;check each incoming string to see if it already had it in the  
&nbsp;&nbsp;&nbsp;&nbsp;pool. With JDK 1.2 came weak references. Now unused interned  
&nbsp;&nbsp;&nbsp;&nbsp;strings will be garbage collected."

> **···**
>
> ara.t.howard@noaa.gov wrote:
> 
> > also, don't forget that symbols are \_\_never\_\_ freed.  
> > this is a severe memory leak:
> > 
> > &nbsp;&nbsp;loop{ Time::now.to\_f.to\_s.intern }

---

<div class="post-metadata">

**Author:** ![Chad\_Perrin1](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Chad\_Perrin1](https://rubytalk.org/u/Chad_Perrin1)\
**Post date:** [29 December 2005 16:50 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/28 "2005-12-29T16:50:14Z")

</div>

> \> As this thread quite well demonstrates,  
> \> a definition for Symbols is quite difficult to come up with.
> 
> A formal definition is difficult to formulate correctly, but what CLHS  
> has is good enough:
> 
> "Symbols are used for their object identity to name various entities  
> in Common Lisp, including (but not limited to) linguistic entities  
> such as variables and functions." --  
> [http://www.lisp.org/HyperSpec/Body/syscla\_symbol.html#symbol](http://www.lisp.org/HyperSpec/Body/syscla_symbol.html#symbol)

Ever since this discussion started, I've wondered if there was some  
relationship between Ruby symbols and Lisp symbols. This is the first  
thing I've seen that seemed to indicate that, though. Prior to this  
quote you provided, everything that has been said in this discussion of  
Ruby symbols has seemed to indicate that there is zero relationship  
between the two.

So tell me: Is a Ruby symbol basically just an object oriented  
implementation of the Lisp concept of a symbol? If that's the case,  
what's up with all this tripe about "a symbol is just a name" and so on?  
I get the distinct impression that either there's no real relationship  
between Ruby symbols and Lisp symbols, or else all the people trying to  
explain Ruby symbols don't much know what they're talking about.

> It is just a way to name/identify things. In RL, you name/identify  
> that collection of atoms 'bird', and another collection of atoms  
> 'cow'. In ruby, you name/identify an object 'host', and another object  
> 'port'. Just as you name parts of collection of atom that you named  
> 'bird' 'wings', 'legs', 'feathers', etc., you name parts of a  
> Hashtable that you named 'ServerConfig' 'host', 'port'.

Until you mentioned the hash table in that paragraph, it didn't make any  
sense at all. I think these analogies are doing a disservice to people  
trying to get a read on what a symbol is in Ruby. You might as well go  
back to trying to explain OOP with truck analogies, which never worked  
worth a damn for explaining the concept either, if you are going to do  
that. It's impressively frustrating trying to make heads or tails of  
this discussion of Ruby symbols: people are full of analogies and  
categorical statements that don't really explain anything, but there  
isn't a whole lot of substance to it.

If you tell me that Ruby symbols are basically just Lisp symbols,  
implemented in an OOP way by way of a symbol class, I think I could  
probably come up with a succinct and clear explanation of what a Ruby  
symbol is that would remove a lot of the ambiguity, but so far I still  
don't know if that's what a Ruby symbol actually is.

> Just as one does not dumbed down "'everything' is an object in ruby",  
> which is a powerful ability, one should not dumbed down "symbols are  
> user-defined identifiers". It's just parts of learning curve.

Dumb down? No, never. Try explaining it, though. Identifier for what?  
While you're at it, define "identifier" within this context.

> **···**
>
> On Thu, Dec 29, 2005 at 11:37:59PM +0900, Yohanes Santoso wrote:
> 
> --  
> Chad Perrin [CCD CopyWrite | [http://ccd.apotheon.org](http://ccd.apotheon.org) ]
> 
> "Real ugliness is not harsh-looking syntax, but having to  
> build programs out of the wrong concepts." - Paul Graham

---

<div class="post-metadata">

**Author:** ![Anonymous\_Coward](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Anonymous\_Coward](https://rubytalk.org/u/Anonymous_Coward)\
**Post date:** [29 December 2005 20:13 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/29 "2005-12-29T20:13:36Z")

</div>

> Eero Saynatkari \<ruby-forum-reg@mailinator.com\> writes:
> 
> \> Yohanes Santoso wrote:  
> \>\> Alex Knaub \<aknaub@gmail.com\> writes:  
> \>\>  
> \>\>\>\> Hi,  
> \>\>\>\>  
> \>\>\>\> I am a Ruby newbie. I wish I didn't post such a simple question here  
> \>\>\>\> but I had to.  
> \>\>\>\> What is the difference between :foo (a keyword) and "foo"(a string).  
> \>\>\>\> Can they be used interchangeably? Are they fundamentally same and is  
> \>\>\>\> the only difference performance?  
> \>\>\>  
> \>\>\> [http://onestepback.org/index.cgi/Tech/Ruby/SymbolsAreNotImmutableStrings.red](http://onestepback.org/index.cgi/Tech/Ruby/SymbolsAreNotImmutableStrings.red)  
> \>\>  
> \>\> What a coincidence. Seems like Jim and I finally had enough of people  
> \>\> conflating symbols and immutable strings on the same day.  
> \>\>  
> \>\> [http://microjet.ath.cx/WebWiki/2005.12.27\_UsingSymbolsForTheWrongReason.html](http://microjet.ath.cx/WebWiki/2005.12.27_UsingSymbolsForTheWrongReason.html)  
> \>  
> \> While, technically speaking, both of you are absolutely and  
> \> undeniably correct (with one correction: Symbols are not Strings),  
> \> such a conflation is the best way to get someone over the  
> \> initial confusion.
> 
> The initial confusion is simply part of the learning curve rubyists  
> must go through along with learning that 'everything' is an  
> object. Dumbing down the situation can bring unexpected result:
> 
> Since we are around Christmas, I'm going to use Santa Claus as an  
> example. To prevent the initial confusion of children finding presents  
> magically appearing before Christmas tree, parents resort to such  
> outlandish notion that that is the work of a joyful guy who has never  
> heard of razor in a red jumper suit and riding reinders-pulled sleigh  
> around the world in one night.
> 
> Some children outgrew that notion. Some don't. Those that don't would  
> either sulk for a long time about the demise of such a joyful guy, or  
> start to think their parents are liars and spend the next untold years  
> of their life learning enough Math and Physics to prove that it is  
> impossible for a guy like Santa Claus to visit all children in one  
> night.

Yes, I like the rationale presented. Perhaps I should have worded  
differently: conflating Symbols with immutable strings (not Strings)  
makes it easier to understand their function in ruby. Any ambiguity  
resulting from this is far easier to correct than it is the assumption  
that Symbols are identifiers or references or variables (rather than  
values, which is the first thing anyone needs to know about Symbols).

> \> As this thread quite well demonstrates,  
> \> a definition for Symbols is quite difficult to come up with.
> 
> A formal definition is difficult to formulate correctly, but what CLHS  
> has is good enough:
> 
> "Symbols are used for their object identity to name various entities  
> in Common Lisp, including (but not limited to) linguistic entities  
> such as variables and functions." --  
> [http://www.lisp.org/HyperSpec/Body/syscla\_symbol.html#symbol](http://www.lisp.org/HyperSpec/Body/syscla_symbol.html#symbol)

This makes no sense to someone just starting.

> It is just a way to name/identify things. In RL, you name/identify  
> that collection of atoms 'bird', and another collection of atoms  
> 'cow'. In ruby, you name/identify an object 'host', and another object  
> 'port'. Just as you name parts of collection of atom that you named  
> 'bird' 'wings', 'legs', 'feathers', etc., you name parts of a  
> Hashtable that you named 'ServerConfig' 'host', 'port'.

This makes sense but, as you notice, you are using 'strings' in  
your explanation 🙂

> Just as one does not dumbed down "'everything' is an object in ruby",  
> which is a powerful ability, one should not dumbed down "symbols are  
> user-defined identifiers". It's just parts of learning curve.

Very good until this part. Symbols, most empathically, are \*not\*  
identifiers (and I am quite sure this was merely a mistype/-thought  
on your part).

> \> To paraphrase fifteen thousand fourty-three mediocre  
> \> comedians over the last three centuries:  
> \>  
> \> "A Symbol is like a word, a sentence, a phrase, a  
> \> description or, perhaps, a name. Except sometimes."
> 
> \>  
> \>\> YS.

E

- --  
"That sort of thing happens in bath tubs maybe. Not in real life."

> **···**
>
> On 2005.12.29 23:37, Yohanes Santoso \<ysantoso-rubytalk@dessyku.is-a-geek.org\> wrote:
> 
> > \>\>\> 2005/12/28, Surgeon \<biyokuantum@gmail.com\>:

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [28 December 2005 20:59 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/30 "2005-12-28T20:59:24Z")

</div>

That's what I suspected as shown in the comments of my last example, but Austin explains it much better. Learn something new all the time... 🙂

James Edward Gray II

> **···**
>
> On Dec 28, 2005, at 2:51 PM, Austin Ziegler wrote:
> 
> > When you do:
> > 
> > &nbsp;&nbsp;&nbsp;attr\_accessor :my\_name
> > 
> > You do NOT get a @my\_name variable. You get two methods: Foo#my\_name  
> > and Foo#my\_name= -- that's it. Consider:
> > 
> > > > class Foo  
> > > > &nbsp;&nbsp;attr\_accessor :bar  
> > > > end
> > 
> > =\> nil
> > 
> > > > baz = Foo.new
> > 
> > =\> #\<Foo:0x2d8aea8\>
> > 
> > Note. Thus far, there's no instance variable @bar on the Foo instance baz.
> > 
> > > > Foo.instance\_methods(false)
> > 
> > =\> ["bar", "bar="]
> > 
> > There's our instance methods.
> > 
> > > > baz.bar = 32
> > 
> > =\> 32
> > 
> > > > baz
> > 
> > =\> #\<Foo:0x2d8aea8 @bar=32\>
> > 
> > Now that we've called Foo#bar= on the baz instance of Foo class, baz  
> > finally has a @bar instance variable. But not a moment before, unless  
> > we instantiate such an instance variable prior to the call of  
> > Foo#bar=.

---

<div class="post-metadata">

**Author:** ![Johannes\_Friestad](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Johannes\_Friestad](https://rubytalk.org/u/Johannes_Friestad)\
**Post date:** [28 December 2005 21:12 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/31 "2005-12-28T21:12:24Z")

</div>

Depends on the call.  
In the case of attr\_\*, it's because you're naming the attribute, not  
the methods or the variable. The convention (and the code behind  
attr\_\*) will do the expansion.  
In instance\_variable\_get(...), you are explicitly looking for a  
variable, and naturally supply the variable name.  
In both cases, the symbol is just a name. What we are naming depends  
on the context.

jf

> **···**
>
> On 12/28/05, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > On Dec 28, 2005, at 2:35 PM, Steve Litt wrote:
> > 
> > \> One thing -- why not some\_call(:@my\_variable)?
> > 
> > This is a fair question I've asked myself once or twice. Ruby seems  
> > to change it's mind on this sometimes too:
> > 
> > \>\> class MyClass  
> > \>\> def initialize( var )  
> > \>\> @var = var  
> > \>\> end  
> > \>\> attr\_reader :var # I guess we're talking about the method here  
> > (no @)  
> > \>\> def fetch( name )  
> > \>\> instance\_variable\_get("@#{name}") # but we need the @ now  
> > \>\> end  
> > \>\> end  
> > =\> nil  
> > \>\> ex = MyClass.new(123)  
> > =\> #\<MyClass:0x32565c @var=123\>  
> > \>\> ex.var  
> > =\> 123  
> > \>\> ex.fetch(:var)  
> > =\> 123
> > 
> > James Edward Gray II

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [28 December 2005 21:51 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/32 "2005-12-28T21:51:29Z")

</div>

i never consider that as an impl - i bet your right though... time for me to  
read the source.

cheers.

-a

> **···**
>
> On Thu, 29 Dec 2005, Johannes Friestad wrote:
> 
> > That's where the 'can be' part comes in 🙂  
> > The point is that symbols support quicker lookup by their nature.  
> > Whether they are quicker in practice will depend on the  
> > implementation. From the timings you give, it looks like symbol lookup  
> > is implemented by converting the symbol to a string and doing string  
> > lookup. Which is obviously not quicker 🙂
> 
> # --
> 
> > ara [dot] t [dot] howard [at] noaa [dot] gov  
> > all happiness comes from the desire for others to be happy. all misery  
> > comes from the desire for oneself to be happy.  
> > -- bodhicaryavatara
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [28 December 2005 22:24 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/33 "2005-12-28T22:24:57Z")

</div>

%w( NORTH SOUTH EAST WEST ).each{|c| const\_set c, c}

😉

-a

> **···**
>
> On Thu, 29 Dec 2005, Dan Diebolt wrote:
> 
> > NORTH = :NORTH  
> > SOUTH = :SOUTH  
> > EAST = :EAST  
> > WEST = :WEST
> 
> # --
> 
> > ara [dot] t [dot] howard [at] noaa [dot] gov  
> > all happiness comes from the desire for others to be happy. all misery  
> > comes from the desire for oneself to be happy.  
> > -- bodhicaryavatara
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Jim\_Weirich1](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jim\_Weirich1](https://rubytalk.org/u/Jim_Weirich1)\
**Post date:** [28 December 2005 22:30 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/34 "2005-12-28T22:30:44Z")

</div>

Jim Weirich wrote:

> > also, don't forget that symbols are \_\_never\_\_ freed.
> 
> True, but when used properly, this is rarely a concern. If they are  
> used as programmer names for things, then the number of symbols is  
> finite and not likely to grow and consume memory as the program runs.

Here I am replying to my own posting, but I think this point could use  
some elaboration.

Why are symbols not garbage collected? Because a symbol represents a  
mapping from a string name to a unique object. Anytime in the execution  
of the program, if that name is used for a symbol, the original symbol  
object must be returned. If the symbol is garbage collected, then a  
later reference to the symbol name will return a different object.  
That's generally frowned upon (although I don't really see the harm. If  
the original symbol was GC'ed, nobody cared what the original object was  
anyways. But that's the way it works).

This might be one area where the "Symbol isa immutable string" meme  
might be doing some real harm. We if think of symbols as strings, then  
we tend to build symbols dynamically like we do strings. This is when  
the memory leak" problem of symbols becomes a problem.

Here's a rule of thumb ... if a programmer never sees the symbol name in  
the code base, then you probably should be using a string rather than a  
symbol.

I'm not sure if this helped, or just muddied the water more.

> **···**
>
> --  
> -- Jim Weirich
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Johannes\_Friestad](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Johannes\_Friestad](https://rubytalk.org/u/Johannes_Friestad)\
**Post date:** [29 December 2005 02:53 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/35 "2005-12-29T02:53:09Z")

</div>

BTW: Ruby version 1.8.2, Win XP Pro, Pentium M 2.0 GHz

jf

> **···**
>
> > and here are some results:  
> > ...

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [29 December 2005 03:10 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/36 "2005-12-29T03:10:56Z")

</div>

\<snip test\>

that way well be true now. however, if you look at my test it goes to some  
lengths to make the test a little more 'real-world':

&nbsp;&nbsp;&nbsp;- creats a large (2 \*\* 13) hash  
&nbsp;&nbsp;&nbsp;- poplate using a semi-random distribution  
&nbsp;&nbsp;&nbsp;- selects keys for lookup in a semi-random distribution  
&nbsp;&nbsp;&nbsp;- fork for each test to isolate tests somewhat  
&nbsp;&nbsp;&nbsp;- disable GC for each test  
&nbsp;&nbsp;&nbsp;- runs each test 4 times

in anycase, all i'm driving at is that a pretty heavy duty test (not saying  
mine is that test) is required to eliminate the differences data distribution,  
gc, and system load have on the results. in particular i can see how a linear  
distribution might have a huge effect - seeing as symbols are essentially  
numbers and hash more predictably whereas making the jump from '9999' to  
'10000' is likely to land in quite a different bucket.

it's nonetheless very interesting to see some tests though.

i use both btw. 😉

-a

> **···**
>
> On Thu, 29 Dec 2005, Johannes Friestad wrote:
> 
> > On 12/28/05, ara.t.howard@noaa.gov \<ara.t.howard@noaa.gov\> wrote:
> > 
> > > On Thu, 29 Dec 2005, Johannes Friestad wrote:
> > > 
> > > > Symbols are (or can be) quicker for hash lookup, since it is sufficient to  
> > > > compare object identity to find whether two symbols are the same, while  
> > > > strings must be compared character by character. You are unlikely to notice  
> > > > the difference unless your program uses hashes heavily.
> > > 
> > > i see this claim all the time but never data supporting it, all my test  
> > > programs have shown the opposite to be true.
> > 
> > Along with Jim and Mauricio, my tests indicate that symbols are  
> > consistently quicker, even on short strings.
> 
> # --
> 
> > ara [dot] t [dot] howard [at] noaa [dot] gov  
> > all happiness comes from the desire for others to be happy. all misery  
> > comes from the desire for oneself to be happy.  
> > -- bodhicaryavatara
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [29 December 2005 03:43 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/37 "2005-12-29T03:43:25Z")

</div>

Hmm, smells like work and documentation combined. Two evils in one quiz. 😉

I suspect that would make for an unpopular topic.

James Edward Gray II

> **···**
>
> On Dec 28, 2005, at 4:18 PM, Dan Diebolt wrote:
> 
> > How about devoting the next Ruby Quiz to coming up with the best-of-class examples, self paced-tutorial and documentation to settle the :symbol vs "string" issue?

---

<div class="post-metadata">

**Author:** ![Edward\_Faulkner](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@Edward\_Faulkner](https://rubytalk.org/u/Edward_Faulkner)\
**Post date:** [29 December 2005 17:09 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/38 "2005-12-29T17:09:40Z")

</div>

Yes, I think Ruby symbols and Lisp symbols are the same thing. The  
issue is just slightly obscured because Ruby glosses over the Lisp  
concept of "quote" by building the quote syntax directly into the  
symbol-literal syntax. :mysymbol means the same thing as Lisp's  
'mysymbol, except the whole thing is treated as a single literal  
token, instead of parsing to (quote mysymbol).

> **···**
>
> On Fri, Dec 30, 2005 at 01:50:14AM +0900, Chad Perrin wrote:
> 
> > If you tell me that Ruby symbols are basically just Lisp symbols,  
> > implemented in an OOP way by way of a symbol class, I think I could  
> > probably come up with a succinct and clear explanation of what a Ruby  
> > symbol is that would remove a lot of the ambiguity, but so far I still  
> > don't know if that's what a Ruby symbol actually is.

---

<div class="post-metadata">

**Author:** ![Yohanes\_Santoso1](https://avatars.discourse-cdn.com/v4/letter/y/ccd318/32.png) [@Yohanes\_Santoso1](https://rubytalk.org/u/Yohanes_Santoso1)\
**Post date:** [29 December 2005 20:07 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/39 "2005-12-29T20:07:35Z")

</div>

Chad Perrin \<perrin@apotheon.com\> writes:

> Prior to this quote you provided, everything that has been said in  
> this discussion of Ruby symbols has seemed to indicate that there is  
> zero relationship between the two.

The first line of my journal (third post in this thread) already  
mentions "The concept of symbols have been popular among the lisp  
community...".

So, there is not zero relationship between the two. Before coming to  
ruby, I was already familiar with elisp and dabled some in CL. A quick  
reading of the then scampy documentation of ruby Symbol revealed that  
they are just different implementations of the same concept. But they  
are not the same. ruby's symbol is CL&elips symbol minus attributes  
(symbol value, property list, etc.)

> So tell me: Is a Ruby symbol basically just an object oriented  
> implementation of the Lisp concept of a symbol?

Yes: different implementations of the same concept. No: CL&elisp's  
Symbol has more functionalities.

> If that's the case, what's up with all this tripe about "a symbol is  
> just a name" and so on?

Because it's true? Because there are people who don't have lisp  
background who are not familiar with what symbol is? Because there are  
people who thinks symbols are magical thingie?

> I think these analogies are doing a disservice to people trying to  
> get a read on what a symbol is in Ruby.

This is true for all analogies. Different people are receptive to  
different analogies. This simply means you are not receptive to the  
analogy I made.

> people are full of analogies and categorical statements  
> that don't really explain anything

The basic message has always been clear: symbol is a mean for you, the  
programmer, to name/identify entities. But somehow, people are having  
difficulty understanding this and asked for more explanations. This is  
when analogies and categorical statements came in.

> > Just as one does not dumbed down "'everything' is an object in ruby",  
> > which is a powerful ability, one should not dumbed down "symbols are  
> > user-defined identifiers". It's just parts of learning curve.
> 
> Dumb down? No, never. Try explaining it, though.

As this and many other previous threads about symbol have shown,  
explaining the concept of symbol is not easy.

> While you're at it, define "identifier" within this context.

To help people understanding the meaning of 'identifier', I've been  
mentioning it together with 'name': 'name/identify' as in "It is just  
a way to name/identify things".

In any case, there is only one definition of identifier so context  
should not influence the meaning of it:

From WordNet (r) 2.0 (August 2003) [wn]:

&nbsp;&nbsp;identifier  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n : a symbol that establishes the identity of the one bearing it  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
YS.

---

<div class="post-metadata">

**Author:** ![Johannes\_Friestad](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Johannes\_Friestad](https://rubytalk.org/u/Johannes_Friestad)\
**Post date:** [29 December 2005 20:44 UTC](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653/40 "2005-12-29T20:44:23Z")

</div>

Yes, the concept of a symbol in Ruby is the same as in Lisp. Just as  
similar as the concepts for integers, strings, arrays and other basic  
constructs.

I think the confusion comes from two areas:

1) Not many languages has 'symbol' as a distinct data type. Java has  
interned strings, which are pretty close to symbols for most practical  
purposes (symbols are atomic, immutable and unique, Java interned  
strings are just immutable and unique), but they are not a separate  
datatype.

2) It is not immediately obvious why symbols are useful. Numbers and  
arrays/lists are easily explained as representing numbers and 'a  
construct to hold multiple values', but there is no such simple  
explanation for symbols. Not that I know of, anyway.

jf

> **···**
>
> On 12/29/05, Chad Perrin \<perrin@apotheon.com\> wrote:
> 
> > Ever since this discussion started, I've wondered if there was some  
> > relationship between Ruby symbols and Lisp symbols..  
> > So tell me: Is a Ruby symbol basically just an object oriented  
> > implementation of the Lisp concept of a symbol?

[Previous page](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653.md?page=1)

[Next page](https://rubytalk.org/t/what-is-the-difference-between-foo-and-foo/23653.md?page=3)
