[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What does "@@" mean?

Zhao Yi

1/18/2009 12:33:00 PM

In a ruby class, what does a variable "@@name" mean? Does it mean static
variable? Does its subclass have access to this field?
--
Posted via http://www.ruby-....

5 Answers

David A. Black

1/18/2009 12:54:00 PM

0

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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

Zhao Yi

1/18/2009 1:07:00 PM

0

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.
--
Posted via http://www.ruby-....

Robert Klemme

1/18/2009 1:27:00 PM

0

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

--
remember.guy do |as, often| as.you_can - without end

Ken Bloom

1/18/2009 6:38:00 PM

0

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;
}



--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Eustaquio 'TaQ' Rangel

1/18/2009 7:08:00 PM

0

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...

Regards!