Symbols

Contents
What a symbol is
Symbols vs strings
Symbols as hash keys
To_sym and to_s

What a symbol is

A symbol is a small fixed name with a leading colon, such as admin or ok. Symbols label things rather than hold text: roles, states, and option names are classic examples.

role = :admin puts role # prints admin

The same symbol always means the same name wherever it appears. That makes symbols cheap to compare and ideal for marking fixed choices in your code.

Symbols vs strings

Strings hold changeable text for messages and user input, while symbols name fixed ideas inside the program. A symbol and a string with the same letters are different objects and never equal to each other.

:admin.to_s # => "admin" "admin" == :admin # => false

Symbols as hash keys

Hashes pair keys with values, and symbols are the most popular keys because they read well and compare fast. Write each pair as name plus colon, then the value.

user = { name: "Ada", role: :admin } puts user[:role] # prints admin

Older code writes the same pair with a rocket between key and value. Both styles build the same hash, but new code prefers the short form above:

old = { :role => :admin } puts old[:role] # prints admin

To_sym and to_s

Two tiny converters move between the worlds. to_sym turns a string into a symbol, and to_s turns a symbol back into a string for printing or joining with other text.

"admin".to_sym # => :admin :admin.to_s # => "admin" puts "role=#{:admin.to_s}" # prints role=admin

Next: Truthiness: only false and nil

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: