From: "Ara.T.Howard" Date: 2005-07-12T09:16:11+09:00 Subject: Re: class_attr_accessor On Tue, 12 Jul 2005, Luke Kanies wrote: > 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... it just ducks into the singleton class of whatever object you use it on. in the case the object is a class and it just so happens that class methods are singleton methods of classes so you end up defining class methods this way > 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. not really. every ruby object has it's own singleton class where one can add methods for only that object: harp:~ > cat a.rb a = Array::new class << a def foobar 42 end end p a.foobar harp:~ > ruby a.rb 42 using this construct in classes just so happens to work nicely since singleton methods of classes are also know as 'class methods' for instances of that class. > I'll begin switching all of my 'def Class.attr...' method defs to this. > :/ there's no need really - but it allows you to do things like this that cannot be done otherwise: class C class << self attr 'a' attr 'b' alias c a alias d b end attr 'a' attr 'b' end so now we have instance method 'a' and 'b' and class methods 'a' and 'b' and aliases for those two class methods 'c' and 'd' respectively. because the 'attr' and 'alias' calls are in class singleton scope here it works. > 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. if they don't it's doubtful that they are real subclasses. most OO theory would suggest that any subclass (subtype) is a specialzation only of the base class - eg it should only ADD new behaviour. be definition that means any existing class vars should be inherited. if you aren't inheriting class vars (and therefore clobbering them) you might want to look at other code sharing methods like mixins since you may not have a true inheritence hierarchy. of course most of my inheritence hierarchies aren't technically 'valid' either ;-) > 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. you have that already class C class << self attr 'foo' end end > 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. it's pure ruby so you can always just cut and paste it. ;-) cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================