# Symbols vs. variable

**URL:** <https://www.ruby-forum.com/t/symbols-vs-variable/191225>\
**Category:** Ruby\
**Created:** [July 7, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/symbols-vs-variable/191225 "2010-07-07T17:51:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 7, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/symbols-vs-variable/191225/1 "2010-07-07T17:51:20Z")

</div>

What is the main difference between using a symbol (begins with :), and  
a variable?

When should we use one over another?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 7, 2010, 6:04pm UTC](https://www.ruby-forum.com/t/symbols-vs-variable/191225/2 "2010-07-07T18:04:57Z")

</div>

On Wed, Jul 7, 2010 at 5:51 PM, Abder-rahman Ali  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is the main difference between using a symbol (begins with :), and  
> a variable?

They are two completely different concepts:

- A symbol is an object. It’s literal representation is a set of  
characters that start with :, but it’s a full object with methods,  
etc. It has some properties that make them different to other objects,  
the most relevant one being that all instances of the literal refer to  
the same object:

irb(main):003:0\> :test.object\_id  
=\> 87218  
irb(main):004:0\> :test.object\_id  
=\> 87218

Compare this to, for example, Strings or Arrays:

irb(main):005:0\> “test”.object\_id  
=\> -610207318  
irb(main):006:0\> “test”.object\_id  
=\> -610213628  
irb(main):007:0\> [].object\_id  
=\> -610222448  
irb(main):008:0\> [].object\_id  
=\> -610232888

- A variable is a reference to an object. When you say

a =

you are creating a local variable that refers to . That  
object could be any object, for example a Symbol:

a = :test

> When should we use one over another?

I don’t think this question has any sense, as they are completely  
different concepts.

Jesus.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 7, 2010, 9:19pm UTC](https://www.ruby-forum.com/t/symbols-vs-variable/191225/3 "2010-07-07T21:19:47Z")

</div>

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 8, 2010, 4:34am UTC](https://www.ruby-forum.com/t/symbols-vs-variable/191225/4 "2010-07-08T04:34:46Z")

</div>

2010/7/7 JesÃºs Gabriel y GalÃ¡n [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> On Wed, Jul 7, 2010 at 5:51 PM, Abder-rahman Ali  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > When should we use one over another?
> 
> I don’t think this question has any sense, as they are completely  
> different concepts.

A better question would be: Why use a symbol? Although, a quick Google  
search will give some good answers.
