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

The variables in Ruby are @, @@ and class instance variable

Post cover: The variables in Ruby are @, @@ and class instance variable
Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
There are several types of variables in Ruby that differ in that, who owns them - to an object, a class, or all descendants at once. Let's deal with examples simply.

@ - instance variable

This is a normal variable that belongs to a specific object. Everybody 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 does not share between objects - that's their personal data.

@@ - class variable

Variable with two dogs (@@) common between all instances and subclasses. It lives inside 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 is better to avoid its use.

Class instance variable - a safe alternative to @@

You can create a variable at class level, but that it should belong only to this class, not to the heirs. This is a normal @variable, but declared at the level of the class itself (and 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 (nil first 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 is checked and when to use
29 Oct 20:47

==, equal?, eql?, === in Ruby: what is checked and when to use

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

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

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

module_function in Ruby: When module methods are available as modular 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 object type?
30 Oct 19:55

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

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

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

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

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

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

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

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

What is 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