From: aero6dof@... (Alan Chen) Date: 2003-08-23T01:58:13+09:00 Subject: Re: Class variables - a surprising result Ged Byrne wrote in message news:<20030822093558.59647.qmail@web10801.mail.yahoo.com>... > If this idiom could be replaced with some clear syntax > I think it would be a good thing. > > > > > > If you want to access these in instance methods, > you need to do: > > > > > > class Foo > > > def instance_meth > > > p self.class.class_eval { @var } > > > end > > > end The beauty of ruby is that you can clean this up if you really want. class Module # generate "static" variables more like C++ or Java def attr_reader_static(*syms) syms.each { |sym| module_eval( %Q[def self.#{sym}; @#{sym}; end] ) } end def attr_writer_static(*syms) syms.each { |sym| module_eval( %Q[def self.#{sym}=(val); @#{sym} = val; end] ) } end def attr_accessor_static(*syms) attr_reader_static(syms) attr_writer_static(syms) end end class Foo attr_accessor_static :staticvar end Foo.staticvar = 11 p Foo.staticvar => 11