What is the difference between symbols and strings in Ruby?
Updated May 17, 2026
Short answer
Strings are mutable, separate objects in memory with different object IDs, whereas Symbols are immutable, unique identifiers that share the same object ID throughout the application's lifecycle.
Deep explanation
In Ruby, strings represent textual data and are mutable; every time you declare a string (e.g., 'hello'), Ruby allocates a new memory address for it. This makes strings expensive if reused frequently. Symbols (e.g., :hello) are immutable scalars. Once created, a symbol is stored in an internal symbol table, and every reference to that symbol points to the exact same memory location. This makes symbols highly performant as hash keys and method identifiers since comparison happens via instant object ID equality instead of character-by-character parsing.
Real-world example
Symbols are extensively used as keys in configuration hashes, parameters in Rails controllers, or attribute accessors (attr_accessor :name) due to memory optimization and fast lookups.
Common mistakes
- Dynamically converting user input into symbols using `to_sym`. Because Ruby (prior to recent garbage collection improvements) did not easily garbage collect old symbols, this could lead to an out-of-memory denial-of-service (DoS) attack.
Follow-up questions
- Are symbols ever garbage collected in modern Ruby?
- How do you freeze a string to make it behave more like a symbol?