On 7/13/06, bwv549 <jtprince / gmail.com> wrote:
> It is simple to set a class constant in ruby:
>
>   class Foo
>     CLASS_CONSTANT = "never to change"
>   end
>
> And accessed like this:
>   Foo::CLASS_CONSTANT
>
> However, I'd like to be able to create a class variable that is easily
> accessible (read/write) by outside users without having to write class
> accessor methods.  How can I do this?
>
> A really ugly way to set the variable without an accessor would be
> this:
>   class Foo; @@class_constant = "newvalue" end
> but that is write only.
>
> The syntax:
>   Foo.class_constant = "newvalue"
> would be preferable, but how to do this without writing accessor
> methods?
>
> Thanks

Here's the Rails implementation if you just want to see one working
implementation.  Note that it allows you to access the class attribute
from the class and from instances (which is similiar to static members
in Java, actually).

http://dev.rubyonrails.org/svn/rails/trunk/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb

- Rob
-- 
http://www.robsanheim.com
http://www.seekingalpha.com
http://www.ajaxian.com