Forum: Ruby What does "@@" mean?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2009-01-18 13:34
In a ruby class, what does a variable "@@name" mean? Does it mean static
variable? Does its subclass have access to this field?
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-01-18 13:57
(Received via mailing list)
Hi --

On Sun, 18 Jan 2009, Zhao Yi wrote:

> In a ruby class, what does a variable "@@name" mean? Does it mean static
> variable? Does its subclass have access to this field?

It's a class variable, which is actually a class-hierarchy variable
(shared between a class and its descendants), and also visible to all
the instances of all of those classes.

In other words, it's a kind of class-hierarchy-scoped global. Think of
it as $$name :-)


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
7fbd0e99b27064b14793f028022974e5?d=identicon&s=25 Zhao Yi (youhaodeyi)
on 2009-01-18 14:08
David A. Black wrote:
> It's a class variable, which is actually a class-hierarchy variable
> (shared between a class and its descendants), and also visible to all
> the instances of all of those classes.
>
> In other words, it's a kind of class-hierarchy-scoped global. Think of
> it as $$name :-)
>
>
> David

Ok thanks. That's why I always got error when access to this variable
outside of the class-hierarchy.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-18 14:30
(Received via mailing list)
On 18.01.2009 14:07, Zhao Yi wrote:
> David A. Black wrote:
>> It's a class variable, which is actually a class-hierarchy variable
>> (shared between a class and its descendants), and also visible to all
>> the instances of all of those classes.
>>
>> In other words, it's a kind of class-hierarchy-scoped global. Think of
>> it as $$name :-)

> Ok thanks. That's why I always got error when access to this variable
> outside of the class-hierarchy.

Actually I would recommend against using this beast.  There are some
subtle issues with regard to definition order.  Better use an instance
variable of the class instance.

Kind regards

  robert
355dfec2a7633db603db2a178bddb631?d=identicon&s=25 Eustáquio Rangel (Guest)
on 2009-01-18 20:09
(Received via mailing list)
On Sun, Jan 18, 2009 at 11:28 AM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> Actually I would recommend against using this beast.  There are some subtle
> issues with regard to definition order.  Better use an instance variable of
> the class instance.

'Beast' was a fun name for them. :-)
You can use like it like this:

http://gist.github.com/48743

Regards!
851acbab08553d1f7aa3eecad17f6aa9?d=identicon&s=25 Ken Bloom (Guest)
on 2009-01-18 20:10
(Received via mailing list)
On Sun, 18 Jan 2009 07:33:29 -0500, Zhao Yi wrote:

> In a ruby class, what does a variable "@@name" mean? Does it mean static
> variable? Does its subclass have access to this field?

"static variable" means different things in different contexts in C and
Java, but @@variables are roughly equivalent to those defined by
following Java code:

public class Foo{
  private static int bar=0;
}
This topic is locked and can not be replied to.