Intern Track Roadmap

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
Free preview · Lesson 1 2 min read

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 lessons
Type to start searching...