From: Nathaniel Talbott Date: 2003-08-23T02:18:05+09:00 Subject: Re: Class variables - a surprising result Alan Chen [mailto:aero6dof@yahoo.com] wrote: > 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 Or even: class Foo class << self attr_accessor :staticvar end end Foo.staticvar = 11 p Foo.staticvar => 11 Nathaniel <:((><