Ruby symbols vs strings
Ruby has two ways to write what looks like the same word: the String "name" and the Symbol :name. This course explains how they differ and when to use each. It covers symbol syntax, mutable strings versus immutable symbols, and object identity checked with object_id and equal?. It shows how interning saves memory and speeds up comparisons, why symbols are the usual hash keys, and where strings fit better. It ends with to_sym, to_s and a small challenge that converts string keys to symbols.
What you'll learn
- ✓Symbol syntax and how the String and Symbol classes differ
- ✓Mutating a string in place versus reassigning a variable
- ✓Object identity with object_id, equal? and ==
- ✓How interning affects memory use and comparison speed
- ✓Choosing between data and labels, including symbol hash keys
- ✓Converting values with to_sym and to_s
Two ways to write "the same word"
Both hold the letters n-a-m-e and sound the same read aloud. They're different kinds of object, though: a String and a Symbol, each with its own rules. This course covers what separates them and when to use each.
Read the full lesson free →Curriculum
7 lessonsTwo ways to write "the same word"
Introduces String and Symbol as two different classes for values that share the same letters.
Mutable strings, immutable symbols
Shows how strings change in place, how mutation differs from reassignment, and why symbols reject mutation.
Object identity: why symbols are "the same"
Uses object_id and equal? to show that every :active is one interned object, unlike string literals.
Memory efficiency and comparison speed
Explains how interning lets symbols save memory and compare by identity, which matters at scale.
When to use symbols: hash keys and identifiers
Separates data from labels to decide between strings and symbols, with hash keys as the main symbol use.
Converting between them: to_sym and to_s
Covers to_sym and to_s for converting in each direction, and why neither changes the original.
Putting it together: choosing the right one
Builds a symbolize_keys method that converts incoming string keys to symbols, followed by a small challenge.