Ruby Symbols vs Strings

Last updated on December 20, 2021

What are Symbols?

A lot of programming languages don’t have Symbols - they only have Strings. In those languages, Strings are used as identifiers as well as storing data.

In Ruby, this logic is split into two separate types - we use Symbols as identifiers and Strings are used for working with data.

If you have ever worked with Ruby or Ruby on rails, you have encountered Symbols. They look like this

:post
:index
:user

For whatever reason, you might be interested to see which Symbols are currently in the memory of your application using the command

Symbol.all_symbols

You should be more interested in understanding what Symbols are, not when to use them. Generally, if you understand what they are, chances are that you will be fine.

Use Symbols when you are sure that the value will remain constant. For example, hash keys.

Use Strings when you want to change the value or you want to use a string method on it.

hash = {
  :name => "Rails Junior",
  :url => "https://railsjunior.com/",
  :monthly_readers => 10000
  }

or using the newer Ruby syntax:

hash = {
  name: "Rails Junior",
  url: "https://railsjunior.com/",
  monthly_readers: 10000
  }

Another common usage of Symbols is for defining the attr_accessor of a class

class User
attr_accessor :first_name
end

new_user = User.new
new_user.name = "Gob"

In Rails, we might use Symbols to define the thing we render - for example partials or actions.

respond_to do |format|
  format.js { render :update }
end

Main differences between Strings and Symbols

Symbols are immutable; Strings aren’t.

Being immutable means that the values won’t change.

Therefore, Symbols are faster than Strings. In fact, Strings are about 1.7 times slower than Symbols!

To delve deeper, let’s open the rails console and define a string and assign it to a variable.

This will create a new object in memory.

If we repeat the same assignment, it will assign the same definition to another object in memory.

foo = "hi"
=> "hi"

foo.object_id
=> 70346754598460

foo = "hi"
=> "hi"

foo.object_id
=> 70346736294700

That’s two different objects for our String - bad for memory.

Let’s repeat the same thing, but use Symbols instead.

foo = :hi
=> :hi

foo.object_id
=> 5286428

foo = :hi
=> :hi

foo.object_id
=> 5286428

Multiple uses of the same Symbol have the same object ID. Therefore, they are the same object!

On the other hand, Strings will be a different object with unique object ID. Therefore, different objects.

Not only that the objects themselves are faster, but also some methods, such as comparing two Symbols, are faster than the equivalent methods for Strings.

Finally, let’s be honest - Symbols looks better and are more readable than Strings.

Convert between Symbols and Strings

Why do you want to convert Symbols to Strings? One of the main reasons is that Symbols only have a subset of the methods that Strings have. For example, Strings have the .split method, while Symbols don’t.

Check out the Ruby docs for full method list of Symbol and String.

Converting between symbols to strings is easy - use the .to_s method.

foo = :hi
=> :hi

foo.class
=> Symbol

foo = foo.to_s
=> "hi"

foo.class
=> String

Converting string to symbols is equally easy - use the .to_sym method.

foo = "hi"
=> "hi"

foo.class
=> String

foo = foo.to_sym
=> :hi

foo.class
=> Symbol

Invite us to your inbox.

Articles, guides and interviews about web development and career progression.

Max 1-2x times per month.