From: Trans Date: 2008-10-23T11:21:55+09:00 Subject: Re: subclass a class in the namespace of the that subclass On Oct 22, 6:09 pm, "David A. Black" 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: > > >  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. > > How about this? > > c = Class.new > X = Class.new(c) > X.class_eval do >    const_set("Y",c) > end > > p X::Y                    # X::Y > p X.superclass            # X::Y > p X.superclass == X::Y    # true > > (I used const_set instead of Y = 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.