From: Zev Blut Date: 2006-08-08T18:36:01+09:00 Subject: Re: Automatic instantiation of subclasses On Tue, 08 Aug 2006 18:19:12 +0900, Kilivor Kante wrote: > First sorry I am a beginner ;-( What I want to do is: automatically > instantiate subclasses of a given class, say, A. > > class A > end > > class A1 < A > end > > class A2 end > > So I want to know the names of A subclasses: A1 and A2. Then instantiate > via reflection. Is it possible? > > Using ObjectSpace it is possible to know all the instantiated objects of > a class. But here no objects yet. This is a fun pattern that I use a lot. Here is a simple code snippet using ObjectSpace: subclasses = [] ObjectSpace.each_object(Class) do |klass| if klass != A && klass.ancestors.include?(A) subclasses << klass end end # Then you can create new instances by calling new on the class subclases.each do |klass| klass.new end Cheers, Zev