What does "@@" mean?

Z

Zhao Yi

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

David A. Black

Hi --

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!
 
Z

Zhao Yi

David said:
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.
 
R

Robert Klemme

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
 
K

Ken Bloom

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top