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


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.