From: "Ara.T.Howard" Date: 2005-07-12T07:06:09+09:00 Subject: Re: class_attr_accessor On Tue, 12 Jul 2005, Jeffrey Moss wrote: > I was playing around with class variables and class instance variables > because I wanted to implement a certain functionality, while not really > understanding the coexistence of the two, I found the reason the two exist is > quite clear, the class instance variables facilitate "subclass slots" (as I > know them from other languages) while the @@ syntax facilitates "class slots" > > I did a google search for "class_attr_accessor" and found that a number of > people have implemented this functionality in their own code. I suggest it > be officially added to the ruby distribution. > > Here is an example: > > class Module > private > def class_attr_accessor(*attrs) > attrs.each {|attr| > module_eval(<<-EOS) > class << self > def #{attr}; @#{attr}; end > def #{attr}=(v); @#{attr} = v; end > end > EOS > } > end > end > > I really don't think a new notation, similar to @@, is necessary, I think > calling obj.class.accessor_name is very nice. In fact having both a > subclass_attr_accessor and a class_attr_accessor wouldn't hurt either. In > that case, class_attr_accessor would just be a way to make @@vars public. > > -Jeff Moss check out http://raa.ruby-lang.org/project/traits/ inheritence is quite tricky with the impl of class_attr_accessor above. cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================