adding attributes (attr_*-style) to single objects

D

David A. Black

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
 
J

Joel VanderWerf

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.
 
J

Joel VanderWerf

Joel said:
David A. Black wrote:
...



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.
 
D

Daniel Brockman

Joel VanderWerf said:
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 => :eek:ld-1, :new-2 => :eek:ld-2

The arrow helps me remember which parameter is which.
 
D

David A. Black

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top