Ruby Basics : Symbol

In this article you will learn the concept of symbol and when it is used in Ruby programs.

Symbol Analogy

Dove Symbol

The dove is a symbol. It represents peace. There is a one-to-one association between the symbol and what it represents.

Ruby Symbol

The symbol identifier begins with a colon. In the IRB console, we can represent dove symbol like this:

 > :dove
 => :dove 

It is unique, as there is only one object corresponding to the dove symbol. We can verify it.

 > :dove.object_id
 => 1175068 
 > :dove.object_id
 => 1175068 
 > :dove.object_id
 => 1175068 
 > :dove.object_id
 => 1175068 

Regardless of how many times you call the object_id, the memory location of the object is the same.

Summary

In this article, you learned about symbol and how it is unique in a running program. Symbols are used as the arguments to methods and as name of methods.


Related Articles


Software Compatibility Best Practices

I spoke to some of the most talented and experienced software developers. I have created a guide that is filled with valuable insights and actionable ideas to boost developer productivity.

You will gain a better understanding of what's working well for other developers and how they address the software compatibility problems.

Get the Guide Now