TExcelTAD
Home › Ruby › Ruby Symbols

Ruby Symbols

A symbol looks like a name with a colon in front: :pending, :name, :red. Symbols are one of Ruby's most distinctive features. They are lightweight, immutable labels — perfect for hash keys, option names, states and method names.

Creating symbols

ruby · try it yourself
status = :pending
p status
p status.class
p :"with spaces"
p "approved".to_sym
p :shipped.to_s
p %i[small medium large]
:pending
Symbol
:"with spaces"
:approved
"shipped"
[:small, :medium, :large]

%i[...] is a shortcut for an array of symbols, just like %w[...] creates an array of strings.

Symbols vs strings

A symbol and a string can both hold the text "name", but they are used differently:

Symbol :name String "name"
Mutable? No, never Yes (unless frozen)
Identity Every :name is the same object Each literal is a new object
Purpose Identifiers, labels, keys Text data shown to users or processed
Comparing Very fast (compares identity) Compares character by character
ruby · try it yourself
p :ruby.object_id == :ruby.object_id
p "ruby".object_id == "ruby".object_id
p :ruby == "ruby"
p :ruby.to_s == "ruby"
true
false
false
true

A symbol is never equal to a string, even when they hold the same text. Convert one of them first.

Tip: A simple rule: if the value is data (a user's name, a message), use a string. If it is a label your code uses internally (a key, a status, an option), use a symbol.

Symbols as hash keys

The most common use of symbols is as hash keys. Ruby even has a short syntax for it — name: "Ada" means :name => "Ada":

ruby · try it yourself
user = {name: "Ada", role: :admin, active: true}
p user[:name]
p user[:role]
p user["name"]     # string key: not found!
p user.keys
"Ada"
:admin
nil
[:name, :role, :active]

Mixing string keys and symbol keys is a classic beginner bug: user["name"] returns nil because the key is the symbol :name.

Symbols as states

Symbols make readable state values, often combined with case:

ruby · try it yourself
def describe(state)
  case state
  when :draft then "Not published yet"
  when :live then "Visible to everyone"
  when :archived then "Hidden, kept for history"
  else "Unknown state"
  end
end

[:draft, :live, :archived, :deleted].each do |s|
  puts "#{s}: #{describe(s)}"
end
draft: Not published yet
live: Visible to everyone
archived: Hidden, kept for history
deleted: Unknown state

Symbols and methods

Method names are symbols too. That lets you ask an object about its methods and call them by name:

ruby · try it yourself
word = "hello"
p word.respond_to?(:upcase)
p word.public_send(:upcase)
p [1, 2, 3].map(&:to_s)
p %w[pear fig].map(&:length)
true
"HELLO"
["1", "2", "3"]
[4, 3]

&:to_s turns the symbol into a block that calls to_s on each element. This &:symbol shorthand is everywhere in idiomatic Ruby.

Symbol methods

Method Example Result
to_s / name :abc.to_s "abc"
to_proc :upcase.to_proc.call("a") "A"
length :hello.length 5
upcase, downcase :ok.upcase :OK
<=> :a <=> :b -1

Test yourself

What does :status == "status" return?

  1. true
  2. false
  3. nil
  4. An error

What does {size: 3} mean?

  1. A hash with the string key "size"
  2. A hash with the symbol key :size
  3. A method call
  4. A keyword argument definition

What does %w[a b].map(&:upcase) return?

  1. ["A", "B"]
  2. [:A, :B]
  3. ["a", "b"]
  4. An error