Hi --
Having a special method to add attributes to Class objects has come up
a lot over the years, and several times recently. As I've said
probably too often, it seems to me to make a special case where there
shouldn't be one. There's really no more reason to have
class_attr_accessor than string_attr_accessor, hash_attr_accessor, or
myclass_attr_accessor.
I'd like to suggest a reformulation of the problem, starting by taking
it down to its roots. The root of the problem is that people want to
add attributes to an object, but don't want to have to open up the
object's singleton class with the "class" keyword.
It seems to me that the best solution, therefore, would be a method
that allows you do exactly that: to add attributes to an object
without having to open up the object's singleton class. What I have
in mind (here using the full "accessor" as an example) is this:
class Object
def add_attr_accessor(syms)
(class << self; self; end).class_eval { attr_accessor(syms) }
end
end
(I assume the naming would be hotly debated, so I'll ignore that for
now 
Here's what it allows you to do:
o = Object.new
o.add_attr_accessor(:x)
o.x = 100
# etc.
and it seamlessly, consistently allows you to do exactly the same
thing for a class object:
class C
add_attr_accessor(:x)
end
C.x = 100
# etc.
I think this is a much more useful and scaleable approach than a set
of purpose-written methods exclusively for the use of Class objects.
David
···
--
David A. Black
dblack@wobblini.net
David A. Black wrote:
...
class C
add_attr_accessor(:x)
attr_accessor(:x)
end
The method names are too close. I would have guessed these did the same
thing.
I like the idea, but why not just make #attr_accessor public, so you can
write:
self.attr_accessor :x
and make it clear that you are not adding an instance method.
Joel VanderWerf wrote:
David A. Black wrote:
...
class C
add_attr_accessor(:x)
attr_accessor(:x)
end
The method names are too close. I would have guessed these did the same
thing.
I like the idea, but why not just make #attr_accessor public, so you can
write:
self.attr_accessor :x
and make it clear that you are not adding an instance method.
Duh. That suggestion made no sense.
Maybe something like
my_attr_accessor :x
to add an accessor to the current self, rather than to instances of the
current self.
Joel VanderWerf <vjoel@path.berkeley.edu> writes:
Maybe something like
my_attr_accessor :x
to add an accessor to the current self, rather than to instances of
the current self.
I'd prefer `singleton_attr_accessor'.
I always define my own accessor-defining methods because I think the
non-word `attr' is just too damn ugly. Among others, I have these:
define-reader
define-writer
define-accessors
So I'd add `define-singleton-accessors'.
By the way, I also do this:
define-aliases :new-1 => :old-1, :new-2 => :old-2
The arrow helps me remember which parameter is which.
···
--
Daniel Brockman <daniel@brockman.se>
I don't think the "my" scales well to non-class objects though:
str = "a string"
str.my_attr_accessor :x
It's not clear here what "my" is (or, to put it another way, what "not
my" would be).
I can see that attr_accessor and add_attr_accessor are close.
However, I'd be glad to enter a new era where people had to be careful
about that one name, if in return we could get some relief from the
confusion that seems to proliferate around this whole thing, spilling
over into singleton classes and class variables.
David
···
On Wed, 13 Jul 2005, Joel VanderWerf wrote:
Joel VanderWerf wrote:
David A. Black wrote:
...
class C
add_attr_accessor(:x)
attr_accessor(:x)
end
The method names are too close. I would have guessed these did the same
thing.
I like the idea, but why not just make #attr_accessor public, so you can
write:
self.attr_accessor :x
and make it clear that you are not adding an instance method.
Duh. That suggestion made no sense.
Maybe something like
my_attr_accessor :x
to add an accessor to the current self, rather than to instances of the
current self.
--
David A. Black
dblack@wobblini.net