From: Patrick Hurley Date: 2005-03-30T03:17:34+09:00 Subject: Re: attr :? On Wed, 30 Mar 2005 02:57:58 +0900, Luke Renn wrote: > What is the proper term for things like attr : and belongs_to > :, etc. How do you code them? I tried googling around, but > since I don't know what to call it, its a little tough. I looked at > the ActiveRecord code for a few minutes and it looks like they're just > class methods, but I don't understand how ruby interprets it i guess. > > Thanks. I am not a Ruby expert (yet :-), but the first thing to realize is that attr, etc are just normal methods. And remembering I am far from a Ruby expert, I wrote the following code for personal exploration on the same topic. It adds attr_class_(reader|writer|accessor) methods to Module so that any other class or module can then provide easy access to class data variables. Note all it does use class eval to "write" the access methods -- I do not know if this is what module does or if there is a substantially better technique. I would prefer to have used a closure over class_eval, but could not figure out how (if it is possible). Patrick class Module def attr_class_reader(sym) self.class_eval <