From: "David A. Black" Date: 2008-10-23T07:09:24+09:00 Subject: Re: subclass a class in the namespace of the that subclass 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.) I don't know what you need it for but if that helps, there it is :-) David -- Rails training from David A. Black and Ruby Power and Light: Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL Advancing with Rails January 19-22 Fort Lauderdale, FL * * Co-taught with Patrick Ewing! See http://www.rubypal.com for details and updates!