From: Zev Blut Date: 2006-08-08T22:12:38+09:00 Subject: Re: Automatic instantiation of subclasses On Tue, 08 Aug 2006 18:32:24 +0900, Mauricio Fernandez wrote: > On Tue, Aug 08, 2006 at 06:19:12PM +0900, Kilivor Kante wrote: >> So I want to know the names of A subclasses: A1 and A2. Then instantiate >> via reflection. Is it possible? > > class A > class << self; attr_reader :subclasses end > def self.inherited(x); (@subclasses ||= []) << x end > end I am sure Mauricio knows this, but just in case others don't. If you use this particular solution a lot you might also want to call super, because your class might inherit from another class that also wants to use the inherited method. A simple change to fix this is: def self.inherited(x); (@subclasses ||= []) << x; super; end Cheers, Zev