Symbols vs Strings
When to use symbols.
Symbols vs Strings is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Symbol?
A Symbol is a lightweight, immutable name written with a leading colon, like :name. Symbols are often used as identifiers and hash keys.
status = :active
puts status
puts status.classSymbols Are Immutable
Unlike strings, symbols cannot be changed. Once :hello exists, it stays the same forever.
puts :hello.frozen?
puts "hello".frozen?Same Symbol, Same Object
Every use of the same symbol points to one shared object in memory. Two equal strings are usually different objects.
puts :ruby.object_id == :ruby.object_id
puts "ruby".object_id == "ruby".object_idWhy Symbols Save Memory
Because identical symbols share one object, using symbols as repeated keys is memory efficient. Strings would create a new object each time.
ids = [:a, :a, :a]
puts ids.map(&:object_id).uniq.lengthConverting Between Them
Use to_sym to make a symbol from a string and to_s to go the other way.
word = "color"
sym = word.to_sym
puts sym
puts sym.to_s.upcaseSymbols as Hash Keys
Symbols are the most common hash keys in Ruby because they are fast and never change.
person = { name: "Ada", age: 36 }
puts person[:name]
puts person[:age]The Shorthand Syntax
When keys are symbols, name: "Ada" is shorthand for :name => "Ada". Both create symbol keys.
a = { color: "red" }
b = { :color => "red" }
puts a == bSymbols in Method Calls
Symbols name methods for tools like send and map(&:method). The &:upcase form turns a symbol into a block.
words = ["a", "b", "c"]
puts words.map(&:upcase).inspectWhen to Use a String
Use a string when the value is data you will display, build, or change: names, messages, file content. Strings have rich text methods.
greeting = "Hello"
greeting << ", world"
puts greetingWhen to Use a Symbol
Use a symbol for fixed identifiers: hash keys, states, option names. They signal intent and never accidentally mutate.
def move(direction)
puts "Moving #{direction}"
end
move(:north)Comparing Symbols and Strings
A symbol and a string with the same letters are not equal. They are different types.
puts :name == "name"
puts :name.to_s == "name"Quick Check
Decide what you know about symbols.
Recap
You now know the difference:
- Symbols are immutable, shared, ideal as keys and identifiers
- Strings are mutable, rich text data
- convert with
to_symandto_s :a == "a"is false
Next: creating and reading hashes.
config = { mode: :fast }
puts config[:mode]Frequently asked questions
Is the “Symbols vs Strings” lesson free?
Yes — the full text of “Symbols vs Strings” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Symbols vs Strings”?
When to use symbols. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Symbols vs Strings” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Symbols vs Strings
- Creating and Reading Hashes
- Hash Iteration
- Default Values and Merging