# More on Symbols

**URL:** <https://www.ruby-forum.com/t/more-on-symbols/172124>\
**Category:** Rails\
**Created:** [July 15, 2009, 8:21am UTC](https://www.ruby-forum.com/t/more-on-symbols/172124 "2009-07-15T08:21:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 15, 2009, 8:21am UTC](https://www.ruby-forum.com/t/more-on-symbols/172124/1 "2009-07-15T08:21:41Z")

</div>

From what I have read, in Ruby a Symbol is prefixed with a colon as  
in :my\_symbol, yet I can do this…

> > :my\_symbol  
> > =\> :my\_symbol
> 
> > my\_variable = :my\_symbol  
> > =\> :my\_symbol
> 
> > :my\_symbol.class  
> > =\> Symbol
> 
> > :my\_symbol.object\_id  
> > =\> 199218
> 
> > my\_variable.class  
> > =\> Symbol
> 
> > my\_variable.object\_id  
> > =\> 199218

Comments?

---

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 15, 2009, 12:30pm UTC](https://www.ruby-forum.com/t/more-on-symbols/172124/2 "2009-07-15T12:30:05Z")

</div>

I think of symbols as similar to numbers. How about:

> > 1  
> > =\> 1
> 
> > my\_variable = 1  
> > =\> 1
> 
> > 1.class  
> > =\> Fixnum
> 
> > 1.object\_id  
> > =\> 3
> 
> > my\_variable.class  
> > =\> Fixnum
> 
> > my\_variable.object\_id  
> > =\> 3

Is it Symbols that you’re questioning, or is it that :my\_symbol and  
my\_variable have the same object\_id?

They have the same object\_id because they’re pointing at the same  
object.

Bryan

---

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 15, 2009, 1:49pm UTC](https://www.ruby-forum.com/t/more-on-symbols/172124/3 "2009-07-15T13:49:32Z")

</div>

what you’ve done in this case is have a variable that holds and points  
back  
to the symbol. in a sense that variable is a symbol but the symbol it  
refers  
to had to be created with the normal Ruby syntax. here’s a good link  
that  
might help you understand all that complexities of how symbols work in  
Ruby:  
[The Ruby\_Newbie Guide to Symbols](http://www.troubleshooters.com/codecorn/ruby/symbols.htm) Beware it’s a  
long  
read. 🙂

hope that helps.

RSL

---

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 19, 2009, 6:07am UTC](https://www.ruby-forum.com/t/more-on-symbols/172124/4 "2009-07-19T06:07:06Z")

</div>

On Jul 15, 8:29 pm, Bryan A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> They have the same object\_id because they’re pointing at the same  
> object.

Agreed.

I was trying to demonstrate that a variable, say the unassuming x,  
could actually turn out to be a Symbol (even though it is not prefixed  
with a colon). However, that is not surprising because:

1. In Ruby one can write

x = :my\_symbol

but can’t write

:my\_symbol = x # This is sort of equivalent to trying to write 5 = x

That is, apart from specifying a Symbol literally (e.g. :my\_symbol)  
one can only specify a Symbol by specifying the variable that  
references it. Like ALL objects in Ruby, to specify the object you  
either specify it literally or specify the variable that references  
it.

1. Since Ruby is a dynamically typed language a variable, say the  
unassuming x again, can reference different classes of objects  
throughout a program. As such, a variable could reference any class  
of object at a particular moment in time, including a Symbol object.

Apologies if this is not a revelation to the reader.

---

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 19, 2009, 6:14am UTC](https://www.ruby-forum.com/t/more-on-symbols/172124/5 "2009-07-19T06:14:07Z")

</div>

On Jul 15, 9:48 pm, RSL [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> here’s a good link that  
> might help you understand all that complexities of how symbols work in Ruby:[http://www.troubleshooters.com/codecorn/ruby/symbols.htmBeware](http://www.troubleshooters.com/codecorn/ruby/symbols.htmBeware) it’s a long  
> read. 🙂

Thanks. Have already read that link; it is very good.

---

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 19, 2009, 6:22am UTC](https://www.ruby-forum.com/t/more-on-symbols/172124/6 "2009-07-19T06:22:18Z")

</div>

On Jul 16, 12:11 am, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This article might help
> 
> [http://talklikeaduck.denhaven2.com/2006/09/13/](http://talklikeaduck.denhaven2.com/2006/09/13/)

I think my understanding is correct and inline with the link you  
gave. However the link’s author did make some important points:

1. Besides the Symbol class, other classes in Ruby such as Fixnum,  
NilClass, TrueClass and FalseClass “ensure that if ‘two’ of their  
instances are equal they are the same object.”

2. Ruby’s freeze method makes a particular instance immutable; adding  
“Beware, though, that this only freezes the object itself and not it’s  
sub-objects, or referenced objects”

---

<div class="post-metadata">

**Author:** ![shusseina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@shusseina](https://www.ruby-forum.com/u/shusseina)\
**Post date:** [July 15, 2009, 4:13pm UTC](https://www.ruby-forum.com/t/more-on-symbols/172124/7 "2009-07-15T16:13:16Z")

</div>

On Wed, Jul 15, 2009 at 2:21 AM, shusseina[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > :my\_symbol.object\_id  
> > > =\> 199218  
> > > my\_variable.class  
> > > =\> Symbol  
> > > my\_variable.object\_id  
> > > =\> 199218
> 
> Comments?

A common Ruby beginner confusion is not understanding the relationship  
between objects and variables.

This article might help

[http://talklikeaduck.denhaven2.com/2006/09/13/](http://talklikeaduck.denhaven2.com/2006/09/13/)

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
