J
Jeffrey Moss
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
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