Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between @@ and @ in Ruby? [duplicate]

I just started learning Ruby and I have been unable to find a good explanation on what is the difference between @@ and @ in terms of class variables. If anyone can provide a basic intuitive example, that would be really great. Also are they interchangeable?

like image 343
bachkoi32 Avatar asked Jun 13 '13 22:06

bachkoi32


2 Answers

A variable prefixed with @@ is a class variable and one prefixed with @ is an instance variable. A great description can be found in this answer: https://stackoverflow.com/a/5890199/1181886

like image 55
Ryan Endacott Avatar answered Oct 08 '22 09:10

Ryan Endacott


@ before a variable name : instance variable (one per instance)

@@ before a variable name : static variable (one per class)

like image 30
Reda Avatar answered Oct 08 '22 08:10

Reda