# Symbols

**URL:** <https://rubytalk.org/t/symbols/17545>\
**Category:** ruby-talk\
**Created:** [10 April 2005 18:04 UTC](https://rubytalk.org/t/symbols/17545 "2005-04-10T18:04:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glenn\_Smith](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@Glenn\_Smith](https://rubytalk.org/u/Glenn_Smith)\
**Post date:** [10 April 2005 18:04 UTC](https://rubytalk.org/t/symbols/17545/1 "2005-04-10T18:04:40Z")

</div>

Nope, still not entirely sure I \*get\* symbols.

I'm writing stuff in rails at the moment, and \*seem\* to have free  
choice as to whether to use something like:

@params[:year]

or

@params['year']

What's the difference? Coming from mainly a VB and PL/SQL (and rarely  
'C') I'm not sure there is an equivalent in these languages? Just not  
sure I get them...

Help?

> **···**
>
> --
> 
> All the best  
> Glenn  
> Aylesbury, UK

---

<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:** [10 April 2005 18:18 UTC](https://rubytalk.org/t/symbols/17545/2 "2005-04-10T18:18:22Z")

</div>

> Nope, still not entirely sure I \*get\* symbols.
> 
> I'm writing stuff in rails at the moment, and \*seem\* to have free  
> choice as to whether to use something like:
> 
> @params[:year]
> 
> or
> 
> @params['year']
> 
> What's the difference? Coming from mainly a VB and PL/SQL (and rarely  
> 'C') I'm not sure there is an equivalent in these languages? Just not  
> sure I get them...

Symbols are immutable, unlike Strings. The biggest difference,  
however, is the conceptual one. Wherever you need to use a  
descriptive value for a variable (basically wherever you would  
use a string in other languages) you have the choice of using  
a String or a Symbol; for you, the programmer, both can convey  
immediate information about the value (:flag\_set or 'flag set'  
is clearer than 1) and they are especially useful as Hash keys.

Symbols are slightly more lightweight than Strings and, along  
with them being the 'Ruby Way', that is the main reason to use  
them.

My rule of thumb is to use Symbols anytime a string representation  
is needed but I will not need to modify the string or output  
it to a user.

> Help?

> Glenn

E

> **···**
>
> Le 10/4/2005, "Glenn Smith" \<glenn.ruby@gmail.com\> a écrit:
> 
> --  
> No-one expects the Solaris POSIX implementation!

---

<div class="post-metadata">

**Author:** ![Bill\_Kelly](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bill\_Kelly](https://rubytalk.org/u/Bill_Kelly)\
**Post date:** [10 April 2005 19:16 UTC](https://rubytalk.org/t/symbols/17545/3 "2005-04-10T19:16:17Z")

</div>

> Nope, still not entirely sure I \*get\* symbols.

More info:  
[http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/113107](http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/113107)

> I'm writing stuff in rails at the moment, and \*seem\* to have free  
> choice as to whether to use something like:
> 
> @params[:year]
> 
> or
> 
> @params['year']
> 
> What's the difference?

In the above usage, I'd guess the difference is "not much", or  
"one fewer character typed". 🙂

Probably (just a guess) the @params behind the scenes is a  
hash that has keys of Strings not Symbols. If this is so, then  
in the above, :year is being converted, probably, with .to\_s  
before being used as a lookup in @params hash anyway.

So from the programmer's point of view, in the above situation,  
the use of the symbol probably just comes down to aesthetics.

But, here's an example showing the speediness of symbol-to-  
symbol comparisons:

> > val = :abcdefghijklmnopqrstuvwxyz

=\> :abcdefghijklmnopqrstuvwxyz

> > t1 = Time.now; 1\_000\_000.times { val == :abcdefghijklmnopqrstuvwxyz }; Time.now - t1

=\> 0.671

vs. Strings:

> > val = "abcdefghijklmnopqrstuvwxyz"

=\> "abcdefghijklmnopqrstuvwxyz"

> > t1 = Time.now; 1\_000\_000.times { val == "abcdefghijklmnopqrstuvwxyz" }; Time.now - t1

=\> 2.283

Regards,

Bill

> **···**
>
> From: "Glenn Smith" \<glenn.ruby@gmail.com\>

---

<div class="post-metadata">

**Author:** ![Glenn\_Smith](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@Glenn\_Smith](https://rubytalk.org/u/Glenn_Smith)\
**Post date:** [10 April 2005 19:07 UTC](https://rubytalk.org/t/symbols/17545/4 "2005-04-10T19:07:30Z")

</div>

Perfect! Thanks muchly

Glenn

> **···**
>
> On Apr 10, 2005 7:18 PM, Saynatkari \<ruby-ml@magical-cat.org\> wrote:
> 
> > Le 10/4/2005, "Glenn Smith" \<glenn.ruby@gmail.com\> a écrit:  
> > \>Nope, still not entirely sure I \*get\* symbols.  
> > \>  
> > \>I'm writing stuff in rails at the moment, and \*seem\* to have free  
> > \>choice as to whether to use something like:  
> > \>  
> > \>@params[:year]  
> > \>  
> > \>or  
> > \>  
> > \>@params['year']  
> > \>  
> > \>What's the difference? Coming from mainly a VB and PL/SQL (and rarely  
> > \>'C') I'm not sure there is an equivalent in these languages? Just not  
> > \>sure I get them...
> > 
> > Symbols are immutable, unlike Strings. The biggest difference,  
> > however, is the conceptual one. Wherever you need to use a  
> > descriptive value for a variable (basically wherever you would  
> > use a string in other languages) you have the choice of using  
> > a String or a Symbol; for you, the programmer, both can convey  
> > immediate information about the value (:flag\_set or 'flag set'  
> > is clearer than 1) and they are especially useful as Hash keys.
> > 
> > Symbols are slightly more lightweight than Strings and, along  
> > with them being the 'Ruby Way', that is the main reason to use  
> > them.
> > 
> > My rule of thumb is to use Symbols anytime a string representation  
> > is needed but I will not need to modify the string or output  
> > it to a user.
> > 
> > \>Help?
> > 
> > \>Glenn
> > 
> > E
> > 
> > --  
> > No-one expects the Solaris POSIX implementation!
> 
> --
> 
> All the best  
> Glenn  
> Aylesbury, UK

---

<div class="post-metadata">

**Author:** ![Nicholas\_Seckar](https://avatars.discourse-cdn.com/v4/letter/n/b77776/32.png) [@Nicholas\_Seckar](https://rubytalk.org/u/Nicholas_Seckar)\
**Post date:** [10 April 2005 20:05 UTC](https://rubytalk.org/t/symbols/17545/5 "2005-04-10T20:05:54Z")

</div>

You are exactly correct. (The class is called HashWithIndifferentAccess..)

The main reason for using strings as keys for indifferent hashes is the fact  
that symbols are not garbage collected. If symbols were used, then  
constructing indifferent hashes with arbitrary keys would (eventually)  
produce the symptoms of a memory leak.

> **···**
>
> On Sunday 10 April 2005 15:16, Bill Kelly wrote:
> 
> > Probably (just a guess) the @params behind the scenes is a  
> > hash that has keys of Strings not Symbols. If this is so, then  
> > in the above, :year is being converted, probably, with .to\_s  
> > before being used as a lookup in @params hash anyway.
> 
> --
> 
> Nicholas Seckar aka. Ulysses

---

<div class="post-metadata">

**Author:** ![Glenn\_Smith](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@Glenn\_Smith](https://rubytalk.org/u/Glenn_Smith)\
**Post date:** [11 April 2005 10:56 UTC](https://rubytalk.org/t/symbols/17545/6 "2005-04-11T10:56:24Z")

</div>

Every day I scratch a little bit more off the surface of Ruby!

Thanks guys!!

> **···**
>
> On Apr 10, 2005 9:05 PM, Nicholas Seckar \<nseckar@gmail.com\> wrote:
> 
> > On Sunday 10 April 2005 15:16, Bill Kelly wrote:  
> > \> Probably (just a guess) the @params behind the scenes is a  
> > \> hash that has keys of Strings not Symbols. If this is so, then  
> > \> in the above, :year is being converted, probably, with .to\_s  
> > \> before being used as a lookup in @params hash anyway.
> > 
> > You are exactly correct. (The class is called HashWithIndifferentAccess..)
> > 
> > The main reason for using strings as keys for indifferent hashes is the fact  
> > that symbols are not garbage collected. If symbols were used, then  
> > constructing indifferent hashes with arbitrary keys would (eventually)  
> > produce the symptoms of a memory leak.
> > 
> > --
> > 
> > Nicholas Seckar aka. Ulysses
> 
> --
> 
> All the best  
> Glenn  
> Aylesbury, UK
