liveBook · Manning

Symbol

As explained in The Well-Grounded Rubyist, Fourth Edition

Symbol is Ruby's built-in class whose instances are immutable symbol values representing distinct names or identifiers.

In Ruby, `:name` is a Symbol object, not a string or a variable. The leading colon creates a symbol literal; quotes allow spaces, as in `:"full name"`. A string becomes a symbol with `to_sym` or `intern`, and a symbol becomes a string with `to_s`. section 8.2

A symbol is immutable: `:abc` cannot acquire another character; `:abcd` is a different symbol. It is also unique: every occurrence of `:xyz` denotes the same object, whereas separate evaluations of `"xyz"` produce separate strings. Ruby therefore has no `Symbol#new` constructor. Think of symbols as integer-like values written with characters, rather than editable pieces of text. section 8.2.1 section 8.2.4

>> "xyz".object_id
=> 2707250
>> "xyz".object_id
=> 2704780
>> :xyz.object_id
=> 160488
>> :xyz.object_id
=> 160488

Compare the object IDs: the two string literals differ, but repeated uses of `:xyz` identify the same object. section 8.2.1

Symbols can look stringlike without becoming mutable strings. For example, `:david.upcase` returns `:DAVID`, while `:david[2]` returns the string `"v"`. The original symbol stays unchanged; there is no in-place `upcase!` counterpart for it. section 8.2.4

Two common places to use symbols are:

  • Method-name arguments: `"abc".send(:upcase)` invokes `upcase`; `attr_reader :age` likewise supplies a name. If a method already accepts a string, converting that string with `to_sym` first is usually unnecessary. section 8.2.3
  • Hash keys: `{ name: "Joe", age: 44 }` uses symbols as keys, so the age is retrieved with `hash[:age]`. section 8.2.3

A Symbol object is distinct from an identifier, even when both have the same spelling. Ruby uses symbols internally to track names of variables, methods, and constants; `Symbol.all_symbols` exposes the resulting symbol table. Be careful when probing it: writing `:abc` in an inclusion test itself introduces that symbol, so the test cannot establish whether `abc` was already present. section 8.2.2

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains literals, conversion, identity, immutability, and the symbol table, but does not develop hash behavior in depth.
Examples
Compares object IDs for string and symbol literals, then uses `send`, `attr_reader`, and symbol-keyed hashes.
What it assumes
Builds on familiarity with strings and method calls while introducing symbol syntax and behavior from first principles.
What you can do after
Apply it via Ruby symbol literals and conversions when passing method names or choosing hash keys.
How it connects
Connects symbols to strings, integers, object identity, identifiers, method dispatch, and hashes.

Also explained in

Compare how 5 books explain symbol →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.