2008/10/21 Joel VanderWerf <vjoel / path.berkeley.edu>:
> Robert Klemme wrote:
>>
>> class Base
>>  class <<self
>>    attr_accessor :foo
>>  end
>>
>>  def foo
>>    Base.foo
>>  end
>>
>>  def foo=(x)
>>    Base.foo=x
>>  end
>> end
>>
>> class A < Base
>> end
>>
>> class B < Base
>> end
>>
>> B.new.foo=123
>> puts A.new.foo
>
> Then you need an instance to set a class attribute.

I thought this was what you wanted since you can use @@foo inside instances.

> What about this?
> (Instance methods would be easy to add.)
>
> class Base
>  def self.foo
>    if self == Base
>      @foo
>    else
>      Base.foo
>    end
>  end
>
>  def self.foo=(x)
>    if self == Base
>      @foo = x
>    else
>      Base.foo = x
>    end
>  end
> end

Yep, that makes access without instances easier.

Anyway, the point was to demonstrate that there is a clear and easy
solution that does not need class variables.  Whether it's mine or
yours does not really matter that much. I for my part would rather
have this solution (which is probably a bit more verbose than the
class variable solution) then resort to class variables because they
make code fragile because the declaration order matters.

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end