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



