Take the 2-minute tour ×

As I'm browsing through a Rails source code, it contains the line:

@@autoloads = {}

What does @@ mean in Ruby?

share|improve this question
just want to add, the general consensus is favor class instance variables over class variables. thinkrelevance.com/blog/articles/2006/11/16/… –  Dty Jun 21 '12 at 5:31
Symbolhound is useful for searching for things like this. –  Andrew Grimm Jun 21 '12 at 22:35

3 Answers 3

up vote 2 down vote accepted

It means to access a class property (a property namespaced to the class), not an instance one (a property that exists for each instantiated object from that class).

In your example, the @@autoloads will persist for the length of your program.

class TestObj
  @@prop = 0
  def get_prop

  def increment_prop
    @@prop += 1   

a = TestObj.new
b = TestObj.new


puts b.get_prop # 1


share|improve this answer
Thank you - I understood that @ were instances variables but am still quite new to Ruby. –  sdasdadas Jun 21 '12 at 5:19
One thing to note is that class variables in a parent class are not inherited by child classes. I've known plenty of new Rubyists that were bit by that. –  x1a4 Jun 21 '12 at 5:39
Class variables are inherited (@@...), but class instance variables (@...) are unique to each class regardless of hierarchy. You'll also get "uninitialized" warnings on the class variables that never happen with instance variables. –  tadman Jun 21 '12 at 8:00

@@ identifies a class variable.

share|improve this answer
Thanks, Google made finding this answer a bit difficult. –  sdasdadas Jun 21 '12 at 5:20

@@ is nothing but indicating a class variable.

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.

Another way of thinking of thinking of class variables is as global variables within the context of a single class.

share|improve this answer

Your Answer


