What is a symbol in Ruby, and when should I use one instead of a string?
A symbol is a name that Ruby keeps as one shared object, written with a colon like :name. Use a symbol for labels that identify something, such as hash keys, options and statuses. Use a string for text that can change or that a person reads. Equal symbols are the same object; equal strings are separate objects.
Cite
Poignant Guide Ruby Q&A, "What is a symbol in Ruby, and when should I use one instead of a string?", https://poignantguide.net/answers/read/what-is-a-symbol-in-ruby-and-when-should-i-use-one-instead-of-a-string/, accessed 2026-11-15.- Odile MarchettiStaffStaff: Ruby basics, objects and the Poignant Guide ·
A minimal example
A string is a piece of text in quotes. A hash is a collection of key and value pairs, written between braces. Here a hash uses symbols as keys:
person = { name: "Ada", city: "London" } p person[:name] # => "Ada" p person["name"] # => nil p person.keys # => [:name, :city]The second lookup returns
nilbecause the key is the symbol:name, not the string"name". Ruby treats those as different things, and that one fact causes most symbol confusion.The Ruby documentation for Symbol puts it this way: Symbol objects represent identifiers inside the interpreter, while String objects represent text or data. An identifier is a name your program uses for something, like a method or a variable.
Why a symbol is different from a string
The same documentation says that the same Symbol object is created for a given name for the whole run of a program. You can see this with
equal?, which asks whether two variables point at the very same object:p :ruby.equal?(:ruby) # => true a = "ruby" b = "ruby" p a.equal?(b) # => false p a == b # => trueTwo strings with the same letters are equal in value but are two separate objects. Two copies of
:rubyare one object. A symbol is also not something you edit. You can add letters to a string with<<, but a symbol has no such method, because it is a label and not a piece of text to change.You can convert in both directions:
p :name.to_s # => "name" p "name".to_sym # => :name p "hello world".to_sym # => :"hello world"The last line shows that a symbol can hold spaces; Ruby then writes it with quotes after the colon, as the literals page describes.
Which should I use?
Situation Use Why Hash keys you write in your own code Symbol Short, and the same object every time Options and keyword arguments Symbol The key: valuesyntax is the same as for symbol keys in a hashA status such as :active or :paused Symbol A fixed label, not text to show A name or message a person will read String It is text, and you may build or change it Data that arrives from a form, a file or a web request String Outside data is text until you convert it The syntax documentation for literals shows the hash form
{ a: 1, b: 2 }and notes that the same syntax is used for keyword arguments in a method call. A keyword argument is a named input you pass likegreet(name: "Ada").Older and newer forms
Code from the guide's era often writes hashes with a hash rocket, as in
{ :name => "Ada" }. That form still works in Ruby 3.3. Thename: "Ada"form is the shorter way to write the same thing. If you want a string key you must keep the rocket:{ "name" => "Ada" }.Mistakes beginners make
- Looking up the wrong kind of key. A hash built with
:nameis not found by"name". Check withp hash.keys; if you see colons, use symbols. - Using symbols for text. A greeting or a sentence is not a label. If you plan to join it, shout it or print it, make it a string.
- Converting everything to symbols. If the data came from a user, keep it as a string. Convert only when you need a fixed set of labels.
- Mixing the two in one hash.
{ name: "Ada", "age" => 36 }is legal and confusing. Pick one style per hash.
Check this in order
- Does your lookup return
nilwhen you expected a value? Runp hash.keys. - Are the keys printed with a colon, such as
:name? Then look them up with a colon. - Are the keys in quotes, such as
"name"? Then look them up with quotes. - Did the data come from outside your program? Convert once, in one place, with
to_symortransform_keys(&:to_sym).
To see what
pprints for a symbol versusputs, read What is the difference between puts, print and p in Ruby?, and if you have not set Ruby up yet, start with How do I install Ruby on a Mac with Homebrew and rbenv?. Keep Chapter 3 of the guide open while you try these lines in irb.Checked on 12 November 2026.
- Looking up the wrong kind of key. A hash built with
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.