Symbols in Ruby

Symbols are immutable, reusable constants represented with a colon prefix. They are often used as keys in hashes or identifiers.

Example:

:name
:age
:city

Why use Symbols?

  • They use less memory than strings
  • They are immutable
  • Faster comparison than strings

In Hashes:

{ name: "Ruby", version: "3.2" }
← PrevNext →