T
Trans
Ever get stuck on a problem, and then after staring at it for way too
long, get disgusted b/c there is no way to do it the way you need to
do it, and none of the workable alternatives quite cut it? Well,
that's been my day.
I know it may seem a bit off the wall. But I want to subclass a class
that is also defined in the namespace of the subclass. In other words:
class X < Y
class Y
end
end
Why do I want to do this? Because my library (X) consists of a few
components, one of them being Y. And I want the end user of the
library to use it by typing "X.new". Now, X is all but the same as Y,
with only with some minor adjustments.
Unfortunately for me there seems to be no way to do this without Ruby
complaining of a superclass mismatch.
In the end my alternatives appear to be:
1) Use Z instead of X and fake it with X:
module X
def self.new
Z.new
end
class Z < Y
end
end
2) Delegate Y in X
class X
def initialize
@y = Y.new
end
def method_missing ...
end
#1 sucks because I'm faking it --X isn't really new. And #2 sucks
because it is nothing but a class wrapped around a single instance
variable --a complete waste of resources.
I imagine there is a tricky dynamic coding way to do it, but that will
screw up my RDocs.
Well, I don't see any solution for it --if you have one I'll be
amazed. But at least I got to vent.
T.
long, get disgusted b/c there is no way to do it the way you need to
do it, and none of the workable alternatives quite cut it? Well,
that's been my day.
I know it may seem a bit off the wall. But I want to subclass a class
that is also defined in the namespace of the subclass. In other words:
class X < Y
class Y
end
end
Why do I want to do this? Because my library (X) consists of a few
components, one of them being Y. And I want the end user of the
library to use it by typing "X.new". Now, X is all but the same as Y,
with only with some minor adjustments.
Unfortunately for me there seems to be no way to do this without Ruby
complaining of a superclass mismatch.
In the end my alternatives appear to be:
1) Use Z instead of X and fake it with X:
module X
def self.new
Z.new
end
class Z < Y
end
end
2) Delegate Y in X
class X
def initialize
@y = Y.new
end
def method_missing ...
end
#1 sucks because I'm faking it --X isn't really new. And #2 sucks
because it is nothing but a class wrapped around a single instance
variable --a complete waste of resources.
I imagine there is a tricky dynamic coding way to do it, but that will
screw up my RDocs.
Well, I don't see any solution for it --if you have one I'll be
amazed. But at least I got to vent.
T.