What are symbols in Ruby?

One of the conceptual sticking points I had when learning Ruby was what symbols actually were.

If you’re new to Ruby and/or Rails, you might have come across something like the following:

[sourcecode language=”ruby”]

params[:user_id]

[/sourcecode]

Within a few minutes, you learn or you are told that it’s a symbol. When I was learning Ruby that didn’t mean a lot to me. It took me a bit to really grok them.

A symbol is just a unique way to represent something in code. Think of it like a constant except the value is simply the name of the constant. An example that really helped was the use of Cardinal directions. North, South, East and West don’t really have “values” in the traditional sense. They represent a direction.

For example, if you were coding a game where you control the movement of a player, you might have the following:

[sourcecode language=”ruby”]

def move_player(direction)

if direction == :north

player.y -= 1

elsif direction == :south

player.y += 1

elsif direction == :east

player.x += 1

else #west

player.x -= 1

end

end

[/sourcecode]

In the above code, we don’t care about what the value of North would be. We just care that the player is moving North. That highlights the main purpose of a symbol.

The most common usage of symbols are as keys in a hash. Like the example above, you just need a unique way of being able to identify something.

Here’s another example:

[sourcecode language=”ruby”]

user = {

:first_name => “Don”

:last_name => “Marges”

:age => 29

}

[/sourcecode]

If you contrast this concept with objects in JavaScript, you see that symbols actually make a lot of sense. In JavaScript, the key could be a string which won’t necessarily be unique. Symbols in Ruby are guaranteed to be unique.

Now, you should be warned that you won’t really see an example like the one above in the wild. It is a common practice to use the shorthand of symbols as keys in Ruby. So the above example would look like the following:

[sourcecode language=”ruby”]

user = {

first_name: “Don”

last_name: “Marges”

age: 29

}

[/sourcecode]

I hope this little tutorial has helped you understand symbols a bit more clearly. I didn’t catch on to them as quickly as a should have when learning Ruby because I came from languages like PHP and JavaScript where there isn’t a concept of a Symbol, so I didn’t really have a frame of reference.

Don Marges

Don Marges

Fullstack Web Developer

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin