Symbols

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.

p :admin.to_s # prints "admin" p "admin" == :admin # prints false

ruby compare.rb

"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.

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

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How is a symbol written?

    Answer

    :admin

    A symbol starts with a colon; quotes make a string and the other signs name a variable or a comment.

  2. What does this program print? role = :admin puts role

    Answer

    admin

    puts prints the name of a symbol without the colon.

  3. What does this program print? p :admin.to_s

    Answer

    "admin"

    to_s builds a string, and p shows its quotes so the type is clear.

  4. What is "admin" == :admin?

    Answer

    false

    A string and a symbol are different objects, so they are never equal.

  5. Which pair builds the same hash as { role: :admin }?

    Answer

    { :role => :admin }

    The rocket form with symbol keys is the older spelling of the same pair.

  6. What does this program print? user = { name: "Ada", role: :admin } puts user[:role]

    Answer

    admin

    The key :role holds the symbol :admin, which puts prints as its name.

  7. Which method turns the string "admin" into a symbol?

    Answer

    to_sym

    to_sym is the converter; to_s goes the other way.

  8. Why are symbols a common choice for hash keys?

    Answer

    Because a fixed name compares cheaply and reads well as a label

    A symbol names one fixed idea, so a lookup compares the same object rather than the letters.

  9. What does this program print? puts "role=#{:admin}"

    Answer

    role=admin

    Interpolation calls to_s on the symbol, which gives its name without the colon.

  10. Which of these values holds changeable text for a user message?

    Answer

    "message"

    A string holds the text; symbols name fixed choices and are not the text itself.

Article author: Arthur Isaev