# Quick question about @ and @@

**URL:** <https://www.ruby-forum.com/t/quick-question-about-and/104414>\
**Category:** Ruby\
**Created:** [May 17, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/quick-question-about-and/104414 "2007-05-17T18:22:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [May 17, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/quick-question-about-and/104414/1 "2007-05-17T18:22:07Z")

</div>

What’s the difference between @ and @@? Specifically, which should I use  
if  
I need a global variable in a non-OOP Ruby program?

Thanks!

Mike S.

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [May 17, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/quick-question-about-and/104414/2 "2007-05-17T18:28:11Z")

</div>

Hi –

On Fri, 18 May 2007, Mike S. wrote:

> What’s the difference between @ and @@? Specifically, which should I use if  
> I need a global variable in a non-OOP Ruby program?

@var is an instance variable (i.e., it represents the private state of  
whatever object is in the role of ‘self’ at the point where @var  
appears). @@var is a class variable, which is a kind of  
de-encapsulated thing shared among a class, its descendants, and all  
the instances of all those classes. There’s no connection or relation  
between @var and @@var.

$var is a global variable.

David

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [May 17, 2007, 6:30pm UTC](https://www.ruby-forum.com/t/quick-question-about-and/104414/3 "2007-05-17T18:30:12Z")

</div>

quoth the Mike S.:

> What’s the difference between @ and @@? Specifically, which should I use if  
> I need a global variable in a non-OOP Ruby program?
> 
> Thanks!
> 
> Mike S.

As I understand it, ‘@’ denotes an instance variable, visible only to  
the  
instance of a class, and ‘@@’ is a class variable, visible to all  
instances  
of the class.

If you need a plain old global try: $foovar

-d

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [May 17, 2007, 6:33pm UTC](https://www.ruby-forum.com/t/quick-question-about-and/104414/4 "2007-05-17T18:33:34Z")

</div>

Mike S. wrote:

> What’s the difference between @ and @@? Specifically, which should I use  
> if  
> I need a global variable in a non-OOP Ruby program?
> 
> Thanks!
> 
> Mike S.

@ = Instance variable  
@@ = Class variable

If you need a global variable in a non-OOP program, I guess I’d suggest  
using a constant (capitalized variable). Here’s an example of @ and @@  
usage:

irb(main):001:0\> class Foo  
irb(main):002:1\> attr\_accessor :a  
irb(main):003:1\> @@b = 0  
irb(main):004:1\> def initialize  
irb(main):005:2\> @a = 0  
irb(main):006:2\> end  
irb(main):007:1\> def add\_a  
irb(main):008:2\> @a += 1  
irb(main):009:2\> end  
irb(main):010:1\> def add\_b  
irb(main):011:2\> @@b += 1  
irb(main):012:2\> end  
irb(main):013:1\> def b  
irb(main):014:2\> @@b  
irb(main):015:2\> end  
irb(main):016:1\> end  
=\> nil  
irb(main):017:0\> one\_foo = Foo.new  
=\> #\<Foo:0x28dc078 @a=0\>  
irb(main):018:0\> two\_foo = Foo.new  
=\> #\<Foo:0x28d9ea4 @a=0\>  
irb(main):019:0\> one\_foo.add\_a  
=\> 1  
irb(main):020:0\> one\_foo.a  
=\> 1  
irb(main):021:0\> two\_foo.a  
=\> 0  
irb(main):022:0\> one\_foo.add\_b  
=\> 1  
irb(main):023:0\> one\_foo.b  
=\> 1  
irb(main):024:0\> two\_foo.b  
=\> 1

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [May 17, 2007, 6:37pm UTC](https://www.ruby-forum.com/t/quick-question-about-and/104414/5 "2007-05-17T18:37:13Z")

</div>

That clears things up a lot. I’ll use $var instead of @var for my  
(usually  
few!) global variables.

Thanks!

Mike S.

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [May 19, 2007, 1:50am UTC](https://www.ruby-forum.com/t/quick-question-about-and/104414/6 "2007-05-19T01:50:39Z")

</div>

Hi,

Am Freitag, 18. Mai 2007, 01:20:26 +0900 schrieb Mike S.:

> What’s the difference between @ and @@?

class C  
@a, @@a = 3, 33

```
def set_a x, y ; @a, @@a = x, y ; end

def dump ; puts "#@a, #@@a" ; end
def self.dump ; puts "#@a, #@@a" ; end

```

end

C.dump  
puts

c = C.new  
d = C.new

c.set\_a 7, 77  
d.set\_a 8, 88

c.dump  
d.dump  
C.dump

The output is:

3, 33

7, 88  
8, 88  
3, 88

Bertram
