liveBook · Manning

Instance variable

As explained in The Well-Grounded Rubyist, Fourth Edition

An instance variable is a Ruby variable identified by a single-at-sign prefix and associated with an individual object.

A ticket needs to remember its own venue and date. Give two tickets different values, and each should return the values it received. Ruby stores that per-object state in instance variables, whose names begin with a single `@`, such as `@venue`. You can assign and read them much like other variables, but an instance variable stays with its object after the method that assigned it returns. section 3.2 section 3.2.1

Give each ticket its own state

class Ticket def initialize(venue, date) @venue = venue @date = date end def venue @venue end end first = Ticket.new("Town Hall", "2025-11-12") second = Ticket.new("Convention Center", "2025-12-13") puts first.venue # Town Hall puts second.venue # Convention Center `Ticket.new` passes its arguments to `initialize`, which saves them on the new object. The `venue` method reads that object's `@venue`; its final expression is the method's return value. The argument, method, and instance-variable names need not match, though matching names make the relationship clearer. section 3.2.1

The rule behind “that object” is `self`: an instance variable belongs to whichever object is `self` when the code runs. Inside a ticket's instance method, `@venue` belongs to that ticket. Inside a class method or at the top level of a class body, an instance variable instead belongs to the class object. Identical spelling does not make variables on different objects the same variable; reading an instance's `@v` after assigning the class object's `@v` yields `nil`. section 5.1.5

A few neighboring features clarify how to use instance variables:

  • A local variable does not preserve a method's state for later calls; an instance variable does. Its single `@` also distinguishes it from a class variable's `@@` and a global variable's `$`. section 3.2 section 1.1.3
  • Use a getter to expose a value and a setter to change it after initialization. Attributes use read and/or write methods around instance variables; `attr_accessor` can create those methods. section 3.3 section 15.3.2
  • An instance variable cannot be shared between a class and its instances, or between two instances. Class variables serve a different, shared-state role. section 5.2.5
class Person
  attr_accessor :name, :age
  def initialize(name)
    @name = name
  end
end
bob = Person.new("Bob")
p bob.instance_variables  #1
bob.age = 50
p bob.instance_variables  #2

Inspect the variables an object has acquired: `@name` is set by `initialize`, while assigning through the generated `age=` accessor adds `@age`. The names returned by `instance_variables` retain their `@` prefixes. section 15.3.2

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains assignment, persistence across calls, ownership through `self`, and inspection with `instance_variables`.
Examples
Uses tickets with distinct venues and dates, a person's persistent name, and runtime inspection of accessor-backed variables.
What it assumes
Assumes basic objects and methods while teaching the `@` identifier form before developing object state and `self`.
What you can do after
Build it from scratch: initialize per-object state, expose or change it through methods, and identify its owner using `self`.
How it connects
Connects instance variables to `self`, object state, initialization, accessors, local variables, and class variables.

Also explained in

Compare how 4 books explain instance variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.