Ruby Symbols vs Strings

Tram Ho

Symbol is a not too strange definition in Ruby. However, when starting with Ruby, you will have ten thousand questions why about Symbol: What is a Symbol? Why is not String ?, what’s the difference between Symbol vs String ?, ….

Today I would like to introduce a little bit about Symbol, and the difference between Symbol and String in Ruby.

What is a symbol?

Symbol is defined as “scalar value objects used as identifiers, mapping immutable strings to fixed internal values.” Put simply, a symbol is an immutable string , often used to represent an object’s identity. In programming, an immutable object is an immutable object, it is created and stays the same until destroyed.

The above example shows that the value of the variable str is a String that can be changed by adding the string to get a new string. But it is not possible to do the same thing with the sym variable which is a Symbol, because Symbol is an immutable object.

Why a Symbol and not a String?

The immutable nature of Symbol makes them very valuable in programming, because when working with mutable objects, it is easy to create bugs that are difficult to detect. So using Symbol instead of String will limit this problem.

Another significant difference between String and Symbol is the way they are stored in memory.

Clearly, every time a String is declared, it will be allocated a new memory, even though previously there was an identical string. However, with Symbol, on the other hand, a symbol declared will be allocated a memory, and they will exist there, then we have declared the same symbols, it will still use the value already existed before, but not allocated new memory.

String can reduce the performance of the program if the String is created and canceled continuously while we can fully take advantage of the string of the same value.

So, depending on the particular case, if possible use Symbol instead of String to improve the efficiency of the program

When to use Symbol

Jim Weirich, the inventor of Rack summarizes the difference between the use of String and Symbol as follows: “If the object’s text content is important, use String. If the object’s identity is important, use the Symbol “

Symbols are useful when you need a unique identifier to store a value, such as a key in a hash. If you use String for hash key then these keys will be different in object as well as in memory storage.

Here h1 and h2 use the key "name" but this key is stored in different memory areas. This will be worse if the program is bloated. To solve this problem, use Symbol for the hash key

However, the above key Hash string problem has been solved by Ruby since version 2.2.0. All keys in the hash are .freeze and reused instead of allocating new memory, https://ruby-doc.org/core-2.2.0/Hash.html#method-i-store-label- Element + Assignment

Another example of using Symbol as the identity of an object in Ruby is getter and setter methods

If a value doesn’t need a change, use Symbol instead of String.

Hope you have a better understanding of Symbol and String in Ruby. Thank for your reading!

Reference article from: https://medium.com/@lcriswell/ruby-symbols-vs-strings-248842529fd9

Share the news now

Source : Viblo