Are class variables (and methods) reconstructed in each class instanciation?

In this example …

class MyClass
@@myClassVar = [0,1,2,3,4,5,6,7,8, … 999]

blah, blah, blah

end

… is the class variable @@myClassVar constructed for each instance
of MyClass, or only once and referenced by all other instances? I
ask because I plan a class with some large class variables and/or
methods and am concerned about the effect on performance of
instanciating very many objects of the class.

Thanks

Chris

class MyClass
    @@myClassVar = [0,1,2,3,4,5,6,7,8, ... 999]

    blah, blah, blah
end

.... is the class variable @@myClassVar constructed for _each_
instance of MyClass, or only _once_ and referenced by all other
instances? I ask because I plan a class with some large class
variables and/or methods and am concerned about the effect on
performance of instanciating very many objects of the class.

Only once per interpreter. Using @@ is an effective way of caching.
-sc

···

--
Sean Chittenden

Hi –

···

On Wed, 10 Jul 2002, Chris Reay wrote:

In this example …

class MyClass
@@myClassVar = [0,1,2,3,4,5,6,7,8, … 999]

blah, blah, blah

end

… is the class variable @@myClassVar constructed for each instance
of MyClass, or only once and referenced by all other instances? I
ask because I plan a class with some large class variables and/or
methods and am concerned about the effect on performance of
instanciating very many objects of the class.

The class variable will be created only once (including any possible
subclasses of MyClass, which will share exactly the same class
variable).

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Thank you Sean and David.

Also thanks Matz and all contributors/collaborators; Ruby rocks!

Yours

Chris