Symbol

In this chapter you 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 chapter, 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.

results matching ""

    No results matching ""