Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does @@ mean in Ruby?

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

@@autoloads = {}

What does @@ mean in Ruby?

like image 307
sdasdadas Avatar asked Oct 08 '22 11:10

sdasdadas


2 Answers

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
      @@prop
  end

  def increment_prop
    @@prop += 1   
  end
end

a = TestObj.new
b = TestObj.new

a.increment_prop 

puts b.get_prop # 1

CodePad

like image 64
alex Avatar answered Oct 13 '22 12:10

alex


@@ identifies a class variable.

like image 33
Henrique Zambon Avatar answered Oct 13 '22 10:10

Henrique Zambon