Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm totally new to the Ruby world, and I'm a bit confused with the concept of Symbols. What's the difference between Symbols and Variables? Why not just using variables?

Thanks.

share|improve this question
    
in Lisp - which Ruby is derived from - a symbol is essentially the datatype for a variable; that means you can manipulate the program in a Reflection-esque fashion as it executes. (not the most coherent explanation, sorry) –  Paul Nathan Dec 8 '10 at 5:41

3 Answers 3

up vote 22 down vote accepted

Variables and symbols are different things. A variable points to different kinds of data. In Ruby, a symbol is more like a string than a variable.

In Ruby, a string is mutable, whereas a symbol is immutable. That means that only one copy of a symbol needs to be created. Thus, if you have

x = :my_str
y = :my_str

:my_str will only be created once, and x and y point to the same area of memory. On the other hand, if you have

x = "my_str"
y = "my_str"

a string containing my_str will be created twice, and x and y will point to different instances.

As a result, symbols are often used as the equivalent to enums in Ruby, as well as keys to a dictionary (hash).

share|improve this answer
    
Very good explanation. Thank you. –  WassiMan Dec 7 '10 at 19:18
    
Just to push @mipadi's point, when you compare a string you need to compare every character against each other. So, comparing identical strings is O(n). Whereas ruby symbols compare object references witch is O(1). –  Jeremy Heiler Dec 7 '10 at 19:19
    
Why does not Python have this? –  Job Dec 8 '10 at 1:51
    
You should be careful to not overuse them also, since, last I knew, but I haven't looked into this in ruby >1.9, symbols don't get gc'd and so can eat up memory –  frogstarr78 Dec 8 '10 at 6:08
    
They behave identically in Smalltalk, except they look like #mySymbol. –  Frank Shearar Dec 8 '10 at 15:33

Symbol in Ruby is basically the same thing as symbol in real world. It is used to represent or name something.

Symbols are very commonly used to represent some kind of state, for example

order.status = :canceled
order.status = :confirmed

You can also look at symbol as instant enum. You don't need to define a symbol, you just use it. This article explains it in great detail.

share|improve this answer
    
Thanks for the link. –  WassiMan Dec 9 '10 at 15:44

Also, I am reading a great book, referred to me by a friend, called Eloquent Ruby. It will explain symbols and many of the other remarkable wonders of the language. I highly recommend it.

share|improve this answer
    
Well, technically, this is not an answer to the question. But I agree, it is a great book, and a great introduction to Ruby, highly recommendable for anyone coding in Ruby. –  Pete May 21 at 9:52
1  
-1 Not an answer. Would be a good comment. –  Michael Durrant Oct 25 at 11:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.