Symbols

Ruby has a special object called a symbol. Symbols are like placeholders for identifiers and strings; they are always prefixed by a colon (:), such as :en and :logos. Most importantly, only one copy of the symbol is held in a single memory address, as long as the program is running. You don’t directly create a symbol by assigning a value to one. You create a symbol by calling the to_sym or intern methods on a string, or by assigning a symbol to a symbol:

name = "Brianna"
name.to_sym # => :Brianna
:Brianna.id2name # => "Brianna"
name == :Brianna.id2name # => true

Get Ruby Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.