Hi --

On Fri, 14 Jul 2006, bwv549 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?

One way or another, if you want a class variable set when you call the
method class_constant=, then that method has to set the variable.

See Ara's answer; you're almost certainly better off using a real
attr_accessor operation on the class object.  (Yes, as I pointed out,
it doesn't match the question exactly, but it's an improvement :-)


David

-- 
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black     => RUBY FOR RAILS (reviewed on
                                     Slashdot, 7/12/2006!)
http://dablog.rubypal.com        => D[avid ]A[. ]B[lack's][ Web]log
dblack / wobblini.net              => me