Symbol (Ruby)

A symbol is an immutable, reusable identifier written with a leading colon, like :name. Symbols are used as lightweight, memory-efficient names — especially for hash keys and method references.

Ruby

A symbol such as :status is created once and reused everywhere it appears, unlike a string which allocates a new object each time. This makes symbols memory-efficient and fast to compare (by identity), which is why they're the conventional choice for hash keys, option flags, and referring to method names.

Symbols are immutable — you can't change :name — whereas strings are mutable. The rule of thumb: use a symbol when the value is an identifier or label that won't change (a key, a state, a method name), and a string when it's actual text data you might manipulate or display.

You can convert between them with :sym.to_s and "text".to_sym. In modern hash syntax, key: value uses symbol keys, and many Ruby/Rails APIs accept symbols to name attributes and options.

Syntax

:symbol_name

Examples

Ruby

person = { name: "Ana", role: :admin }
puts person[:name]
puts :admin == :admin
puts :hello.to_s.upcase

Output:

Ana
true
HELLO

Learn it in a lesson

See also

Back to the Dictionary