Skip to content
Lesson 2 of 15

Step 1 of 5 · Reading · ~1 min

Learn

Getting 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.

Up nextNumbers and MathGetting Started

Discussion

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

Sign in to post a comment or reply.

Loading…