From: Jeffrey Moss Date: 2005-07-12T09:06:56+09:00 Subject: Re: class_attr_accessor The @@class_var syntax is unique in that you can assign a value to a class and it is propogated down. With class instance vars, class << self; attr_accessor :class_var; end this will only give you a "slot", you still have to populate the slot with something. So for a subclass to inherit values, it would need to have something populating the subclasses values each time the class is inherited presumably in the "inherited" method or manually as in Ara's "init" method. The same goes for an instance attribute, it needs to be initialized in the initialize method, presumably, you can't assign defaults with the attr_accessor method. By my definition, a "slot" is something that holds data, a "accessor" is merely a way to access that slot. I get the term "slot" from the dylan programming language, but it probably was coined long ago. -Jeff ----- Original Message ----- From: "Luke Kanies" To: "ruby-talk ML" Sent: Monday, July 11, 2005 5:52 PM 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 > >