LearnThatStack Ace your next interview
Ruby · question
Question 3 of 91

What are symbols in Ruby and when should you use them?

beginner
← All Ruby questions
Re-explain
Visual ↓

A symbol is an immutable name written with a leading colon, like :status. Every use of the same symbol refers to the same object in memory.

Ruby stores each symbol once and represents it internally by an integer ID. That saves memory and makes comparing two symbols fast, because Ruby only checks identity.

Use symbols for hash keys, method names and fixed labels such as :active or :pending. Use strings for text that changes or comes from users. Without the frozen_string_literal magic comment, two identical string literals are two separate objects, so the second comparison below is false.

:status.object_id == :status.object_id  # true
"status".object_id == "status".object_id  # false
# Common usage
user = { name: "John", age: 30 }  # symbols as hash keys
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
See it - interactive diagram
One Symbol object vs two String objects
Two uses of :status:status.object_id == :status.object_id
Uses in code
first use1
second use1
Objects
1Symbol
text:status

Ruby stores :status once, so both uses point at one object and the comparison is true.

Two "status" literals"status".object_id == "status".object_id
Uses in code
first use2
second use3
Objects
2String
text"status"
3String
text"status"

Without frozen_string_literal, each literal makes a new String, so this comparison is false.

Every :status is one shared object. Without the frozen_string_literal comment, two "status" literals are two separate objects.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Ruby cheatsheet.

← Back to all Ruby questions
Pro · $10/mo

78 of 91 Ruby answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime