Depends what you mean. attr_accessor works with instance attributes. What do you consider a class attribute?
You can certainly do this:
class Foo
class << self
attr_accessor :bar
end
end
But what you will be getting and setting is the instance attribute of the eigenclass(?) and certainly not a @@bar class attribute. (At the Foo instance level you would access it using `myfoo.class.bar`.)
On the other hand, since class attributes are rather compromised, this might arguably be better...
On the third hand, however -- any complex structure on the class itself should really live in a separate object; it's considered by some to be a code smell. So maybe a rethink might be in order.
This question doesn't make much sense to me...
attr_accessor is used with instance variables.
Instance variables cannot be accessed from class methods.
What is the "attr_accessor for class methods"
supposed to do?
I suspect you are not very clear about basic concepts
(classes, instances, instance variables, constants,
instance vs. class methods, ...) and would suggest you
read some introductory tutorials on these topics and
how to create classes in Ruby.
* how can I use attr_accessor in initialize() ?
(The args to attr_accessor are given when calling it)
Are you sure you need to dynamically create instance variables?
Maybe you could simply use a hash internally (@attributes, @fields),
and use the arguments in initialize as hash keys.
You could define # and #= methods to access those fields.