Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Types  ›  Lesson

Strings and Symbols

Strings 12 minStrings hold text that people read or that changes, and double quotes allow interpolation, while symbols are immutable names, each a single shared object, used for keys and labels.

Strings and Symbols

Bean Cart's menu shows text to customers, like "Oat Milk Latte", and the code also needs short internal labels such as "which drink is this?". Ruby has a type for each job: strings for text that people read or that changes, and symbols for fixed names inside the program.

The rule

Strings are text you show; symbols are names you use.

A String is a sequence of characters in quotes. The kind of quote matters:

drink = "latte"
puts "One #{drink}, please"   # One latte, please
puts 'One #{drink}, please'   # One #{drink}, please

Double quotes allow interpolation with #{...} and escapes like \n for a newline. Single quotes print exactly what you typed. Many teams use double quotes by default, for consistency.

Strings are objects with many methods, and they can change:

drink.length          # => 5
drink.upcase          # => "LATTE"
drink + " to go"      # => "latte to go"   (new string)
drink << " to go"     # drink itself is now "latte to go"
"ab" * 3              # => "ababab"
drink.include?("go")  # => true

A Symbol starts with a colon: :latte, :mocha. It is a lightweight, immutable name. Every :latte in the program is the very same object, while two "latte" strings you write separately are two different objects:

:latte.equal?(:latte)     # => true  (same object)
"latte".equal?("latte")   # => false (two objects)
"latte" == "latte"        # => true  (same content)

Symbols are the usual choice for hash keys and option names, because they are fast to compare and never change:

MENU = { latte: 4.50, mocha: 5.00 }   # keys are :latte and :mocha

Converting between them is one method: :latte.to_s gives "latte", and "latte".to_sym gives :latte.

The shortcut

Ask "Will a person read it, or will it change?" If yes, use a String. If it is a fixed label that the program uses to pick something, such as a key, a status or an option, use a Symbol.

Choosing types for one Bean Cart orderStep 1 of 4
  1. The customer's name

    "Priya" is typed by a person and printed on the cup: String.

That last step is the classic trap: "latte" and :latte are different objects of different types, so a hash does not treat them as the same key.

Drills

Answer fast — trust the rule.

quick drill

With drink = "tea", what does puts 'A #{drink}' print?

quick drill

Which is best as a hash key for an order's status?

quick drill

What does "latte".equal?("latte") return?

quick drill

With MENU = { latte: 4.50 }, what does MENU["latte"] return?

quick drill

After drink = "latte" and drink << "!", what is drink?

Remember

  • Strings are text you show; symbols are names you use.
  • Ask "will a person read it, or will it change?" Yes: String. Fixed label: Symbol.
  • Trap: "latte" and :latte are different hash keys, and single quotes skip interpolation.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy is the price nil?

MENU = { latte: 4.50 } and MENU["latte"] returns nil. Explain why, how to fix it, and when you would choose a symbol over a string.