Variables and Symbols
How Ruby names things, and the symbol type that trips up most newcomers coming from other languages.
2 min read
Ruby's variables are simple on the surface, but the language adds one type most newcomers haven't seen before: the symbol.
Local variables
name = "Ada"
age = 36
is_admin = trueNo type keyword, no let/const/var — just assignment. Ruby convention is snake_case for variable and method names (unlike JavaScript's camelCase), and this is enforced by community style more than the language itself.
Variable scope by sigil
Ruby signals a variable's scope by its first character, not by where it's declared:
local_var = "only visible in this scope"
@instance_var = "belongs to one object"
@@class_var = "shared across all instances of a class"
$global_var = "visible everywhere -- avoid this"
CONSTANT = "conventionally never reassigned"You'll mostly use plain local variables and @instance_variables (covered properly in the object-oriented section). $global variables exist but are almost never good practice — they make code hard to reason about because any file could be changing them. CONSTANT (capitalized names) signals "this shouldn't change," though Ruby only warns rather than forbids reassignment.
Symbols: Ruby's lightweight identifiers
A symbol looks like :name — a colon followed by a name. It's easy to mistake for a string, but it behaves very differently:
status = :active
puts status.class # Symbol
"active".object_id == "active".object_id # false -- two different string objects
:active.object_id == :active.object_id # true -- the same symbol, every timeEvery time you write the string literal "active", Ruby can create a new String object. Every time you write :active, Ruby reuses the same Symbol object. That makes symbols faster to compare and cheaper on memory, which is exactly why Ruby uses them heavily as hash keys and for things that act like fixed labels — statuses, option names, method names — rather than as actual textual content a user might see or edit.
user = { name: "Ada", role: :admin }
puts user[:name] # AdaThat { name: "Ada", role: :admin } hash uses the modern shorthand syntax for symbol keys — name: "Ada" is exactly equivalent to :name => "Ada". You'll see the shorthand form in almost all modern Ruby code.
A rule of thumb
Reach for a symbol when the value is a fixed, internal identifier your program branches on (:pending, :active, :cancelled). Reach for a string when it's text — something typed by a user, displayed on screen, or concatenated and manipulated. Mixing the two up isn't a syntax error, but :active == "active" is false, and comparing a symbol to a string by accident is a genuinely common source of "why isn't this condition matching" bugs for people new to Ruby.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.