From: Luke Kanies Date: 2005-07-12T08:52:31+09:00 Subject: Re: class_attr_accessor On Tue, 12 Jul 2005, Ara.T.Howard wrote: > right you are. it bit me a few times and was ending up writing things like > > class A > class << self > attr 'a' > attr 'b' > def init > self.a = 42 > self.b = 'forty-two' > end > def inherited subklass > subklass.init > super > end > end > self.init > end > > class B < A > self.b = 42.0 > end > > all over the place. The whole '<< self' syntax still confuses me; considering how often I could use code that's the equivalent of this, I should obviously learn to understand it... The newer pickaxe book implies that a new "virtual" class is created when using this syntax; is that true when operating on Class instances? That is, if I do this, are there somehow two A instances, one of them "virtual" and with the methods I've added? I doubt it, but the book leaves it a bit unclear. I'll begin switching all of my 'def Class.attr...' method defs to this. :/ > which starts being kinda ugly. i found that i almost always wanted class > varibles to essentially call 'super' when not over-ridden - in otherwords to > get a reasonable value from the parent class - but this is not possible with > either @@vars or @vars without some trickery. so i wrote it once and called > it traits. ;-) Interesting; I use class instance variables all over the place -- literally in just about every Ruby project I do -- and I almost never have subclasses inheret values from parent classes. Just having a builtin simple class-equivalent to the attr methods would be very useful to me, which I think was the point of the initial post. I love how Ruby's classes are just normal objects, but the fact that I have to manually define all of these attr_accessors for the classes themselves gets pretty annoying. I'll check into 'traits', but I don't think I can use it for this project, since I have to mostly stick to libs that are part of the standard library. -- We're not surrounded, we're in a target-rich environment! --------------------------------------------------------------------- Luke Kanies | http://reductivelabs.com | http://config.sage.org