From: Robert Dober Date: 2006-04-11T04:18:15+09:00 Subject: Re: Adding in class attribute with a Module ------=_Part_4067_8143081.1144696692121 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 4/10/06, Gregory Seidman wrote: > > On Tue, Apr 11, 2006 at 02:19:30AM +0900, Robert Dober wrote: > } On 4/10/06, Gregory Seidman wrote: > } > > } > On Mon, Apr 10, 2006 at 11:58:43PM +0900, TRANS wrote: > } > } > Is it possible to add class attributes (cattr_accessor) using a > } > module? > } > } > If so could you give an example? > } > } > } > } There is no "Ruby Way" to do this b/c Ruby's module methods aren't > } > } inherited by classes that include them. Though I've asked for a way > } > } man times now! > } > [...] > } > } Only ugly meta-programming hacks will give the sought behavior --an= d > } > } even then their are corner-case complications. > } > > } > Does this count as ugly meta-programming? > } > > } > module M > } > def self.included(klass) > } > class << klass > } > attr_accessor :m > } > end > } > end > } > end > } > } It is much prettier than the following ugly meta-programming, but I hav= e > no > } clue how it works !!! > } Could you explain please? > > The first thing to understand is that when you define a class, e.g. class > Foo; ... end, you are just setting a constant (Object::Foo, in this case) > to a new instance of a Class. Just as you can add new methods to an > instance of any other kind of object with class << obj; ... end, you can > add new methods to Foo. When a module is included, its self.includedmetho= d > is called with the Class instance it is being included into as an > argument. I see thank you about the explanation of that hook. I am defining self.included to add an attribute accessor to that Class > instance. No of course not, because it is equivalent to class < } Robert > [...] > --Greg Robert -- Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_4067_8143081.1144696692121--