On Thu, Jun 12, 2008 at 12:50 AM, Greg W. [email protected]
wrote:
– gw
–
Posted via http://www.ruby-forum.com/.
It is most unfortunate that class variables are still advocated so much
:(.
The last but one line shows one of the problems of class variables,
they violate the principle that a base class shall never be touched by
the implementation of its subclasses - I know there is a classy name
for this but I forgot it 
--------------------------- 8< ---------------------------
203/79 > cat class_vars.rb && ruby class_vars.rb
vim: sw=2 ts=2 ft=ruby expandtab tw=0 nu syn=on:
file: class_vars.rb
class Base
@@cvar=:Base
@cinstvar=:base
class << self
attr_reader :cinstvar
def cvar; @@cvar end
end
end
class Sub < Base
@@cvar = :Sub
@cinstvar = :sub
end
puts “@@cvar in Sub: #{Sub.cvar}”
puts “@cinstaver in Sub: #{Sub.cinstvar}”
puts "===“20
puts "@@cvar in Base: #{Base.cvar}”
puts “@cinstaver in Base: #{Base.cinstvar}”
@@cvar in Sub: Sub
@cinstaver in Sub: sub
@@cvar in Base: Sub*
@cinstaver in Base: base
--------------------------- 8< ------------------------------------
So the basic advice is to use Class instance variables, in your case
that might be written as
class Something
@some_data = [1,2,3]
class << self; attr_reader :some_data end
end
you can of course access them directly in class methods either defined
with the self idiom
def self.print_data; puts @some_data end
or via the singleton class idiom
class << self
def print_data; puts @some_data end
end
HTH
Robert
–
http://ruby-smalltalk.blogspot.com/
As simple as possible, but not simpler.
Albert Einstein