What are symbols in Ruby and when should you use them?

Beginner

Answer

Symbols are immutable, reusable constants represented internally by an integer value. They're often used as hash keys, method names, or status identifiers because they're memory-efficient - the same symbol always references the same object in memory.

:status.object_id == :status.object_id  # true
"status".object_id == "status".object_id  # false
# Common usage
user = { name: "John", age: 30 }  # symbols as hash keys