All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

Variables in Ruby: @, @@ and class instance variable

Post cover: Variables in Ruby: @, @@ and class instance variable
This content has been automatically translated from Ukrainian.
In Ruby, there are several types of variables that differ by who they belong to - an object, a class, or all descendants at once. Let's break it down simply with examples.

@ - instance variable

This is a regular variable that belongs to a specific object. Each instance of the class (object) has its own copy of such a variable.
class User
  def initialize(name)
    @name = name
  end

  def name
    @name
  end
end

u1 = User.new("Oleh")
u2 = User.new("Ira")

u1.name  # => "Oleh"
u2.name  # => "Ira"
Each user has their own name. @name is not shared between objects - it is their personal data.

@@ - class variable

A variable with two at signs (@@) is shared among all instances and subclasses. It lives within the entire class hierarchy.
class Animal
  @@count = 0

  def initialize
    @@count += 1
  end

  def self.count
    @@count
  end
end

class Dog < Animal; end

a1 = Animal.new
d1 = Dog.new

Animal.count  # => 2
Dog.count     # => 2
Both Animal and Dog see the same @@count variable. If a subclass changes it, it affects the parent class. Because of this, @@ is considered a dangerous and outdated practice - it's better to avoid using it.

Class instance variable - a safe alternative to @@

You can create a variable at the class level, but so that it belongs only to that class, not to its descendants. This is a regular @variable, but declared at the level of the class itself (not inside initialize).
class User
  @count = 0

  def self.add_user
    @count += 1
  end

  def self.count
    @count
  end
end

class Admin < User; end

User.add_user
User.count   # => 1
Admin.count  # => nil
In this example, User has its own @count, and Admin has a separate one (initially nil until defined). This makes the code safe and predictable.
Like it?React
🧵

This post doesn't have any additions from the author yet.

How self, protected, and private work (Ruby)
Oct 28, '25 13:52

How self, protected, and private work (Ruby)

Нотатки про Ruby та RoR
Include, Extend, Prepend in Ruby: how they work and what the difference is
Oct 29, '25 21:20

Include, Extend, Prepend in Ruby: how they work and what the difference is

Нотатки про Ruby та RoR
Singleton class (eigenclass) in Ruby: what it is and why it is needed
Oct 29, '25 18:29

Singleton class (eigenclass) in Ruby: what it is and why it is needed

Нотатки про Ruby та RoR
What happens if you call [1, 2, 3].map(&Person)
Oct 29, '25 17:54

What happens if you call [1, 2, 3].map(&Person)

Нотатки про Ruby та RoR
Oct 28, '25 14:42

How &:to_s works in Ruby and what Symbol#to_proc is

Нотатки про Ruby та RoR
The difference between blank?, present?, empty? and nil? in Ruby
Oct 30, '25 21:06

The difference between blank?, present?, empty? and nil? in Ruby

Нотатки про Ruby та RoR
&& vs and — the difference in Ruby that can break your code
Oct 30, '25 20:23

&& vs and — the difference in Ruby that can break your code

Нотатки про Ruby та RoR
is_a?, kind_of?, instance_of? — how does Ruby check the type of an object?
Oct 30, '25 19:55

is_a?, kind_of?, instance_of? — how does Ruby check the type of an object?

Нотатки про Ruby та RoR
What is memoization in Ruby?
Oct 30, '25 10:17

What is memoization in Ruby?

Нотатки про Ruby та RoR
module_function in Ruby: when module methods are available as module methods and as functions
Oct 29, '25 21:53

module_function in Ruby: when module methods are available as module methods and as functions

Нотатки про Ruby та RoR
==, equal?, eql?, === in Ruby: what they check and when to use them
Oct 29, '25 20:47

==, equal?, eql?, === in Ruby: what they check and when to use them

Нотатки про Ruby та RoR
What is Middleware in Ruby on Rails and when is it used
Nov 4, '25 10:39

What is Middleware in Ruby on Rails and when is it used

Нотатки про Ruby та RoR
What is the Vanilla Rails approach?
Nov 14, '25 16:48

What is the Vanilla Rails approach?

Нотатки про Ruby та RoR
What is Elasticsearch and how does it work?
Nov 22, '25 12:35

What is Elasticsearch and how does it work?

Нотатки про Ruby та RoR
What is a time-series database?
Nov 22, '25 12:42

What is a time-series database?

Нотатки про Ruby та RoR
Nov 22, '25 12:49

What is VACUUM in PostgreSQL?

Нотатки про Ruby та RoR