Trans
10/23/2008 2:22:00 AM
On Oct 22, 6:09=A0pm, "David A. Black" <dbl...@rubypal.com> wrote:
> Hi --
>
>
>
> On Thu, 23 Oct 2008, Trans wrote:
> > 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:
>
> > =A0class X < Y
> > =A0 =A0class Y
> > =A0 =A0end
> > =A0end
>
> > 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 =A0typing "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.
>
> How about this?
>
> c =3D Class.new
> X =3D Class.new(c)
> X.class_eval do
> =A0 =A0const_set("Y",c)
> end
>
> p X::Y =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# X::Y
> p X.superclass =A0 =A0 =A0 =A0 =A0 =A0# X::Y
> p X.superclass =3D=3D X::Y =A0 =A0# true
>
> (I used const_set instead of Y =3D c because that created a top-level
> constant.)
(see my response to the Captain)
> I don't know what you need it for but if that helps, there it is :-)
Ok. Well I'll spill... I've been working on an "uber" commandline
parser:
class Usage < Command
class Command
class Option
class Argument
Thus Usage is the main toplevel command. A command can have
subcommands, options, arguments... so forth and so on.
T.