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
Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
In Ruby, there are several types of variables that differ in to whom they belong - 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 a 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 exists 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 variable @@count. 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 it is defined). This makes the code safe and predictable.

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

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

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

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

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

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

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

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

What is memoization in Ruby?

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

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

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

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

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

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

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is Middleware in Ruby on Rails and when is it used
04 Nov 10:39

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

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

What is the Vanilla Rails approach?

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

What is Elasticsearch and how does it work?

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

What is a time-series database?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
22 Nov 12:49

What is VACUUM in PostgreSQL?

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