57

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.

devurs
  • 103
  • 4
wassimans
  • 1,173
  • 2
  • 10
  • 12
  • 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 08 '10 at 05:41

3 Answers3

80

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).

mipadi
  • 7,493
  • 36
  • 35
  • 1
    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 Dec 07 '10 at 19:19
  • Why does not Python have this? – Job Dec 08 '10 at 01: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 08 '10 at 06:08
  • They behave identically in Smalltalk, except they look like `#mySymbol`. – Frank Shearar Dec 08 '10 at 15:33
  • @frogstarr78 - Unless you are programatically generating symbols (interning strings without checking them first), you don't have to worry about making too many symbols. – Justin L. Feb 03 '11 at 20:13
  • @Job - strings in Python are immutable, and so they point to the same object in memory the same way symbols in Ruby do. – Abbafei Apr 07 '11 at 01:32
  • @Abafei: No, that *might* be the case (for literals) but almost certainly not for strings generated at runtime! @Wassim: There is no connection between a string and a symbol whatsoever - unless you count the possibility to get the symbol's name back as string, but just because you can get an integers representation as string doesn't mean it's connected to a string either, does it? A symbol is an object, that you refer to via a name `:name`. You will always get the same object when using the same name, always a different object when using a different name. – phant0m Jul 24 '11 at 20:43
19

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.

Jakub Arnold
  • 521
  • 1
  • 6
  • 12
1

Usually, variables tend to be confused with strings, but I can understand you thinking it like a variable. It's understandable. Think of it this way:

The status of a player in a game is represented by a number. 1 means alive, 2 means unsure, 3 means dead. This can easily be replaced by symbols. The symbols could be :alive :unsure and :dead. To check if a player is alive, instead of doing this:

if player_status == 1

You could do this:

if player_status == :alive

sirsnow
  • 31
  • 2