Ruby Symbol and String Differences 

Woon Ket
2 min readDec 12, 2013

--

To many beginners in Ruby programming, the differences of Symbol and String can often be vaguely understood. There are key attributes of Symbol that make it different from String and useful in Ruby. This topic is often asked in preliminary job interview, and one who intends to develop a career in Ruby shall master it. In this post, the differences of Symbol and String are listed and briefly described to give readers a quick grasp of the idea.

Symbol always start with a colon ( : )
Symbol can be generated using the :name and :“string” literals syntax and by the various to_sym methods. Symbol is also restricted to Ruby variable naming rules.

Symbol is immutable
Symbol is an object whose state cannot be changed after construction. As an example, characters cannot be appended to a Symbol such as :ruby << :first. Also, Symbol doesn’t contain some String methods that modify its own object.

puts “fun”.upcase!# => FUNputs :fun.upcase!#NoMethodError: undefined method `upcase’ for :fun:Symbol

Symbol is Unique
When a particular Symbol is constructed, there can be only one copy of it stored at the heap (computer memory) at a given time. When you see :apple in different places, there are actually referencing a same Symbol object. In contrast, the notation “apple” that appears in two places are referencing two different String objects. To illustrate, try the following in your interactive Ruby shell (irb):

# Note: Your object IDs will likely be different that shown below
puts “Rubyist”.object_id
puts “Rubyist”.object_id
puts “Rubyist”.object_id
# => 2260958840
# => 2260952700
# => 2260946560
puts :Rubyist.object_idputs :Rubyist.object_idputs :Rubyist.object_id# => 457068# => 457068# => 457068

Symbol saves memory
The above example shows that a particular Symbol shares the same object ID and therefore the same memory space at a given time. For String, its object is mutable and new copy is instantiated whenever it is accessed. In this setting, Symbol is more efficient in term of memory space.

Symbol is not marked for garbage collection (destruction)
String objects are marked for garbage collection by Ruby interpreter when they are no longer referenced. However, Ruby doesn’t mark Symbol for garbage collection. This facilitates Symbol reuse.

Symbol is faster when it is accessed
Because of the above reasons, Symbol works more efficiently and faster than String. An example is when Symbol is used as hash key in hash lookup. It can be proven by benchmarking:
# Note: Your recorded performance will be different
require ‘benchmark’hashSymKey = { :milk => 1}
hashStringKey = {“milk” => 1}
symKeyPerf= Benchmark.realtime do 500_000.times { hashSymKey[:milk] }endstringKeyPerf= Benchmark.realtime do 500_000.times { hashStringKey[:”milk”] }endputs “Symbol key performance: #{symKeyPerf} seconds.”puts “String key performance: #{stringKeyPerf} seconds.”#=> Symbol key performance: 0.0783460140228271 seconds.
#=> String key performance: 0.119017839431763 seconds.

The above hash lookup operation can also be replaced with “puts” or compare, “==” operations on Symbol and String to show the performance differences.

Summary
Because of the Symbol characteristics discussed above, it is commonly used as hash key or method argument in the Rails framework. In fact, Symbol helps program to run efficiently.

--

--