What does @@variable mean in Ruby? What does @@variable mean in Ruby? ruby ruby

What does @@variable mean in Ruby?


A variable prefixed with @ is an instance variable, while one prefixed with @@ is a class variable. Check out the following example; its output is in the comments at the end of the puts lines:

class Test  @@shared = 1  def value    @@shared  end  def value=(value)    @@shared = value  endendclass AnotherTest < Test; endt = Test.newputs "t.value is #{t.value}" # 1t.value = 2puts "t.value is #{t.value}" # 2x = Test.newputs "x.value is #{x.value}" # 2a = AnotherTest.newputs "a.value is #{a.value}" # 2a.value = 3puts "a.value is #{a.value}" # 3puts "t.value is #{t.value}" # 3puts "x.value is #{x.value}" # 3

You can see that @@shared is shared between the classes; setting the value in an instance of one changes the value for all other instances of that class and even child classes, where a variable named @shared, with one @, would not be.

[Update]

As Phrogz mentions in the comments, it's a common idiom in Ruby to track class-level data with an instance variable on the class itself. This can be a tricky subject to wrap your mind around, and there is plenty of additional reading on the subject, but think about it as modifying the Class class, but only the instance of the Class class you're working with. An example:

class Polygon  class << self    attr_accessor :sides  endendclass Triangle < Polygon  @sides = 3endclass Rectangle < Polygon  @sides = 4endclass Square < Rectangleendclass Hexagon < Polygon  @sides = 6endputs "Triangle.sides:  #{Triangle.sides.inspect}"  # 3puts "Rectangle.sides: #{Rectangle.sides.inspect}" # 4puts "Square.sides:    #{Square.sides.inspect}"    # nilputs "Hexagon.sides:   #{Hexagon.sides.inspect}"   # 6

I included the Square example (which outputs nil) to demonstrate that this may not behave 100% as you expect; the article I linked above has plenty of additional information on the subject.

Also keep in mind that, as with most data, you should be extremely careful with class variables in a multithreaded environment, as per dmarkow's comment.


@ - Instance variable of a class
@@ - Class variable, also called as static variable in some cases

A class variable is a variable that is shared amongst all instances of a class. This means that only one variable value exists for all objects instantiated from this class. If one object instance changes the value of the variable, that new value will essentially change for all other object instances.

Another way of thinking of thinking of class variables is as global variables within the context of a single class.Class variables are declared by prefixing the variable name with two @ characters (@@). Class variables must be initialized at creation time


@@ denotes a class variable, i.e. it can be inherited.

This means that if you create a subclass of that class, it will inherit the variable. So if you have a class Vehicle with the class variable @@number_of_wheels then if you create a class Car < Vehicle then it too will have the class variable @@number_of_wheels