From: "David A. Black" Date: 2005-07-12T20:25:55+09:00 Subject: adding attributes (attr_*-style) to single objects 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