What does @@ mean in Ruby?

When I look at the Rails source code, it contains the line:

@@autoloads = {}

What does @@Ruby mean ?

+5
source share
3 answers

This means access to the property of the class (the name of the property placed in the class), and not the instance (the property that exists for each instance of the object from this class).

Your example @@autoloadswill be saved for the length of your program.

class TestObj
  @@prop = 0
  def get_prop
      @@prop
  end

  def increment_prop
    @@prop += 1   
  end
end

a = TestObj.new
b = TestObj.new

a.increment_prop 

puts b.get_prop # 1

Codepad

+2
source

@@ identifies the class variable.

+1
source

@@ is just an indication of a class variable.

A class variable is a variable that is shared between all instances of the class. This means that for all objects created from this class, there is only one variable value.

Another way of thinking about class variables is with global variables in the context of one class.

+1
source

All Articles