Skip to content

Latest commit

 

History

History
27 lines (19 loc) · 921 Bytes

symbols.md

File metadata and controls

27 lines (19 loc) · 921 Bytes

Symbols

Symbols are immuable identifiers commonly used in hashes or to refer to method names. They are constructed with the syntax :some_symbol, or with the .to_sym and .intern methods. Being immutable, once created they cannot be changed.

When used for hash keys, symbols can take on a different syntax (resembling Python dictionaries). The : is moved to the end of the key and the => is dropped, like such:

numbers = {
  one: 1,
  two: "two",
  three: 3,
}

Symbols should be used over strings wherever possible. Iterating over the above hash using string keys would take twice as long.

The Symbol class

Strictly speaking, a symbol is an instance of the Symbol class. It cannot be publically instanted, but is implicitly done so when a new symbol is created.

Sources