What if your program could remember names just once and never get confused by changes?
Why Symbol type and immutability in Ruby? - Purpose & Use Cases
Imagine you have a list of names you use repeatedly in your program, like keys for a dictionary. You write each name as a string every time you need it.
Writing the same string over and over wastes memory and slows your program. Also, strings can be changed by mistake, causing bugs that are hard to find.
Symbols are like names that never change and are stored only once. Using symbols saves memory and makes your program faster and safer.
hash = { 'name' => 'Alice', 'age' => 30 }
puts hash['name']hash = { name: 'Alice', age: 30 }
puts hash[:name]Using symbols lets your program run faster and use less memory by reusing the same unchangeable names everywhere.
When building a website, you often use keys like :title or :author many times. Symbols keep these keys efficient and safe throughout your code.
Symbols are unique, unchangeable names stored once in memory.
They save memory and speed up your program compared to repeated strings.
Symbols help avoid bugs by being immutable (unchangeable).