-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 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! > > It might seem easy to glaze by this, thinking nothing of > cattr_accessor, a seemingly esoteric method since it's not included in > core Ruby, but you will find it in both Rail's Active Support and Ruby > Facets. And in fact, you've brought up a nice example of how this play > out b/c cattr_accessor and it's ilk really don't do anything very > esoteric under the hood. I'm going to layout what cattr_accessor > essentially does* so all can see this for what it is and what happens > when you try to REUSE structures of this nature. > > For a class, it is simply: > > class C > def self.m ; @m ; end > def self.m=(x) ; @m=x ; end > def m ; self.class.m ; end > def m=(x) ; self.class.m=x ; end > end > > Hence > > C.m = 10 > C.m #=> 10 > c = C.new > c.m #=> 10 > c.m = 20 > c.m #=> 20 > C.m #=> 20 > On a total side note, why do you have cattr_* methods affecting instance methods as well ? class C cattr_accessor :m end C.m = 10 c = C.new puts c.m # => 10 I would expect it to work for "C.m" and "C.m=" but not for "c.m" I would expect: NoMethodError: undefined method `m' for #<C:0xb7d3103c> from /home/zdennis/.irbrc:4:in `method_missing' from (irb):5 from :0 Perhaps I have wrong expectation of cattr_* . I am just asking at this point though to better understand thought behind it. Zach -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFEOoKXMyx0fW1d8G0RAmiHAJ9KTcVR3wogDAWDEkvYEYtBJAJKFACcC/9T Rxg1qQquejXwvmS6hH57aB0= =Eq3D -----END PGP SIGNATURE-----