Blog Details

Compare “Strings” And :Symbols In Ruby

In this blog we are going to know about

  • What a symbol is? 
  • What a string is ?  
  • Difference between string and symbol 
  • How to convert string and symbol?

A lot of  programming languages don’t have symbols, they only have strings . Other languages use strings as identifiers as well as storing data.

In ruby this logic is split into two types, symbols as identifiers and strings are used for working with data.

What are strings in ruby?

In ruby , string is a sequence of one or more characters. It may consist of  numbers , letters and symbols. Apart from other languages, strings are mutable.

i.e. strings can be changed in place instead of creating new strings.

my_name = "hey, my name is ruby"
my_age = "i am very old "
#output
#"hey, my name is ruby"
#"i am very old"

What are symbols in ruby?

Symbol objects represent names and some strings inside the Ruby interpreter. They are generated using the: name and : “string” literals syntax.

The symbol is written with a preceding colon ( : )

p :hello
p :"welcome to ruby world"
#output:-
#hello
#"welcome to ruby world"

Difference between string and symbol

  • Strings are mutable , symbols are immutable .
  • Immutable means that value won’t change
  • Therefore symbols are faster than strings , in fact they are 1.7x faster than strings.
  • Strings need more space than symbols.
  • Strings need more space where symbols need less space , this is a reason why symbols are fast.

An example is given below how strings are stored in memory.

name = "virat kohli"
name = "virat kohli"
name = "virat kohli"
#suppose we give different id to different memory space
# virat kohli = 1
# virat kohli = 2
# virat kohli = 3
#as we can see if i write virat kohli 3 times
#it stores each of them in  difference memory space

Example of symbol is given below :-

name = :viratkohli
name = :viratkohli
name = :viratkohli
#now in this i write viratkohli 3 times
#but this will allocate only one space in memory
#this is the beauty of symbol

We can use object_id method to check where the actual object points to. In case of string it points to different memory object even if the value is same and in case of symbol it points to same object in memory so it has same object_id.

See below example.

3.2.2 :003 > "string".object_id
 => 144960 
3.2.2 :004 > "string".object_id
 => 148060 
3.2.2 :005 > "string".object_id
 => 151160 
3.2.2 :006 > :symbol.object_id
 => 2506588 
3.2.2 :007 > :symbol.object_id
 => 2506588 
3.2.2 :008 > :symbol.object_id
 => 2506588 

Like this we can see the difference between strings and symbols and how they both work differently from each other at the memory stage.

How to convert 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 have this method.
  • Conversion between symbols to strings is easy – use the .to_s method.
greet = :hi
=> :hi

greet.class
=> Symbol

greet = greet.to_s
=> "hi"

greet.class
=> String

Conversion from string to symbols is equally easy – use the .to_sym method.

greet = "hi"
=> "hi"

greet.class
=> String

greet = greet.to_sym
=> :hi

greet.class
=> Symbol

These are the main difference between strings and symbols

In Conclusion :-

In the world of Ruby, strings and symbols may seem akin, yet they dance to different tunes. Strings, dynamic and mutable, adapt to change, ideal for dynamic data. Symbols, unyielding and memory-efficient, find their place in unchanging roles like identifiers.

In a nutshell, use strings for flexibility, adapting to shifting content, and symbols for constancy, ensuring efficiency and immutability. As our Ruby journey unfolds, mastering these nuances lets us compose elegant, purposeful code. Happy coding!

Humive is a cutting-edge software company that specialises in developing innovative digital solutions for the new era, with a strong focus on upcoming technologies.

Contact the technical experts at  Humive  today and elevate your valuable business to new heights  with uniquely crafted solutions.