On Fri, Mar 02, 2007 at 05:20:11AM +0900, james.d.masters / gmail.com wrote:
> Something that is unique to a class but inherits with nice syntax is
> what I'd like to see (and I'm fairly sure is the behavior used in
> other OOP languages).  For example, assume that the behavior that I'm
> referring to uses a triple "@" prefix to initialize ("@@@").  Then:
> 
> class A
>    @@@per_class = 1
>    attr_accessor :per_class
> end
> 
> A.per_class         #=> 1
> 
> class B < A
> end
> 
> B.per_class         #=> 1
> B.per_class = 2     #=> 2
> A.per_class         #=> 1

So how would it work? B.per_class and A.per_class are clearly different,
since they end up pointing at different objects. Does the fact that
@@@per_class is undefined in B mean that the value in A is used?

In that case, you need something like a class hierarchy search to find the
instance variable. How about this:

class A
  def self.per_class
    self.ancestors.each { |k|
      k.instance_eval { return @per_class if defined? @per_class }
    }
  end
  def self.per_class=(x)
    @per_class=x
  end
end

puts "hello"
A.per_class = 1

class B<A
end

puts A.per_class  # 1
puts B.per_class  # 1
B.per_class = 2
puts A.per_class  # 1
puts B.per_class  # 2

Clearly, this could be wrapped up as

    class_attr_accessor :per_class

or similar.

If this gets rid of the @@ syntax from the language, then I'm in favour :-)

Regards,

Brian.