Today I learned about symbols in ruby.
- Ruby strings are mutable unlike python strings (and C strings literals) They are allocated in the heap and can be changed over time.
"Hello"[0] = 'h'
, exactly like[1,2,3][0] = 0
in python, is legitimate in ruby because strings are mutable, However:Hello[0] = "h"
is not.- Symbols are some sort of immutable strings, they are more efficient than strings because they are stored in the stack and they don’t change references.
- Symbols are used as key in hashes, in general when we value identiy over data. because they prevent reallocatng string every time.