Symbol vs String
In this page:
Symbol vs String
Symbols (:name) are immutable and memory-efficient identifiers best used for things like hash keys, method names, and fixed sets of options, while strings ("name") are for actual text data that may change or be manipulated. A common rule of thumb: if the value represents an identifier or category rather than user-facing text, a symbol is usually the better fit. Converting between them is easy with .to_sym and .to_s.
Note: Rule of thumb: use a symbol for identifiers/categories (like :active), and a string for actual text data (like a person's name).
Example: Symbol vs String
status = :active # identifier -- symbol fits well
name = "Alice" # actual text data -- string fits well
puts status.to_s
puts name.to_sym.inspect
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: