Strings & Symbols

Ruby is a dynamic, beginner-friendly programming language, and text is everywhere in real programs — Strings hold readable text while Symbols are fast, immutable labels.

Learn Strings & Symbols in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll build and transform strings, use interpolation, and understand exactly when to reach for a symbol instead.

What You'll Learn in This Lesson

1️⃣ Working with Strings

Strings hold text. The friendliest way to build them is interpolation : inside double quotes, {'# '} runs Ruby code and inserts the result. Because a String is an object, it comes with dozens of useful methods you call with a dot.

We used .inspect on the split result so you can see the array with its brackets and quotes clearly.

2️⃣ Symbols: Lightweight Labels

A Symbol is written with a leading colon, like :active . Think of it as a fixed name rather than editable text. Its superpower is identity: every mention of :active is the same object , so symbols are fast to compare and cheap to reuse. You'll see them constantly as hash keys and status flags.

Your turn. Build an ID badge that mixes strings and a symbol. Replace the three TODO values, then run it.

Turn a title into a URL-friendly slug using downcase and gsub . Run it with ruby slug.rb .

📋 Quick Reference — Strings & Symbols

Practice quiz

How is a Symbol written in Ruby?

  • With double quotes
  • With single quotes
  • With a leading colon, like :name
  • With a trailing colon

Answer: With a leading colon, like :name. A Symbol is written with a leading colon, such as :name or :active.

Why are two identical symbols faster to compare than two identical strings?

  • Every reference to the same symbol is the SAME object
  • Symbols are sorted
  • Symbols are shorter
  • Strings are not comparable

Answer: Every reference to the same symbol is the SAME object. Each identical symbol is literally the same object in memory, so comparison and storage are cheap.

What does "hi".object_id == "hi".object_id evaluate to?

  • true
  • nil
  • an error
  • false

Answer: false. Two string literals are two separate objects, so their object_ids differ — the result is false.

What does :hi.object_id == :hi.object_id evaluate to?

  • false
  • true
  • nil
  • an error

Answer: true. :hi is always the same single object, so the object_ids match and the result is true.

Which kind of string literal performs interpolation with #{...}?

  • Double quotes
  • Single quotes
  • Both
  • Neither

Answer: Double quotes. Only double-quoted strings interpolate; single quotes treat #{...} as literal text.

How do you convert the string "active" into a symbol?

  • "active".to_symbol
  • Symbol("active")
  • "active".to_sym
  • :active.to_s

Answer: "active".to_sym. to_sym converts a string to a symbol; "active".to_sym gives :active.

What does :a == "a" return?

  • true
  • false
  • nil
  • an error

Answer: false. A symbol and a string are different types, so :a == "a" is false.

Are Symbols mutable or immutable?

  • Mutable
  • Mutable only in blocks
  • Depends on the value
  • Immutable (frozen)

Answer: Immutable (frozen). Symbols are immutable, lightweight labels — they are frozen and cannot be changed.

What is the recommended use for a Symbol versus a String?

  • Symbols for editable display text
  • Symbols for fixed identifiers like hash keys; Strings for arbitrary text
  • Always use symbols
  • Always use strings

Answer: Symbols for fixed identifiers like hash keys; Strings for arbitrary text. Use symbols for fixed, known identifiers (like :name); use strings for arbitrary or user-supplied text.

What does "hi"[0] return?

  • "i"
  • 0
  • "h"
  • "hi"

Answer: "h". Strings are zero-indexed, so [0] grabs the first character, "h".