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.
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?
To quote Dave Thomas in
http://www.codegeneration.net/tiki-read_article.php?articleId=9&PHPSESSID=a594d9ec9586b7a0e307f3f4f05e6a85
"You can sense the code’s resistance: things that should be easy
turn our hard, and things that should be easy to change turn in to
nightmares.[…] Quite often the problem lies not with the details of
the code, but instead with something larger, such as the decision to
use a particular technique. "
This is what it feels like at the moment, but I can’t see what else
I should be using.
For a bigger overview of my problem: I am trying to control an
application with a Hierarchical State Machine, and I am trying to
take advantage of the specialisation aspects of the class hierarchy
to create the states. I had to provide a mechanism to navigate
between states, so from that viewpoint the fit was imperfect. Now
it looks worse.
Any ideas? Apart from “If you’re in a hole, stop digging”
Hugh