From: "David A. Black" Date: 2003-12-10T03:04:16+09:00 Subject: Re: Inheriting variables, super, and "not super"? Hi -- On Wed, 10 Dec 2003, Hugh Sasse Staff Elec Eng wrote: > Is there a way in a method to say > "ignore this if called as super"? > > I am trying to inherit variables from a superclass, but I may need > them different in the subclass, so I can't use class variables. Might you make use of instance variables of the classes themselves? That might be a cleaner way to preserve state on a per-Class basis. (I don't know whether it fits in with the whole picture though.) > Instance variables are not passed on to subclasses, so to initialize > them I need to call super within initialize... > > Also, at present, some of my initialization code sets up other > things which make sense for an instance of that class, but not when > I'm using an instance of a subclass. > > Is there an idiom like > > class Y < Z > def initialize() > @y_var = 0 > if self.class == Y > y_init > end > end > end > > > class X < Y > def initialize > super > @x_var = 0 > end > end > > Or does this show I doing something horribly wrong? I have a feeling that if you're looking at two classes whose objects have such different needs, it may be that the case for having one of them be a subclass of the other is not all that strong. Do you think a module-centered design would work better? You could un-verticalize (?) the structure, and bring in what each class or even each object needs. David -- David A. Black dblack@wobblini.net