Skip to content
Variables and Symbols
step 1/5

Reading — step 1 of 5

Learn

~1 min readGetting Started

Ruby variables don't need declarations or types. Just assign:

name = "Alice"
age = 30
active = true
nothing = nil

The casing of the first character determines the kind of variable:

  • name — local variable
  • @name — instance variable
  • @@name — class variable
  • $name — global
  • Name — constant (by convention)

Symbols are interned identifiers — like strings, but with one distinguishing feature: there's only one copy of each in memory. Use them for fixed-string keys in hashes or to name things:

status = :active
user = { name: "Alice", age: 30 }   # symbol keys

Symbols are hashable, fast to compare, and immutable.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…