From: Joel VanderWerf Date: 2004-11-05T08:21:39+09:00 Subject: Re: How to enumerate nested classes Tim Hunter wrote: > I have a class nested in a module: > > module Foo > class Bar > ... buncha stuff here > end > end > > Within Foo::Bar there are many nested classes, not necessarily descended > from Bar, simply contained within its namespace. > > I would like to write a test that ensures that each class contained within > Foo::Bar has a method named deep_copy() somewhere in its inheritence > hierarchy. I'd like to write the test without explicitly listing all the > nested classes. That is, if I add another class to Foo::Bar, then the test > automatically checks it as well. > > I thought I could do this via ObjectSpace.each_object(Foo::Bar) but that > doesn't work. I also tried getting a list of Foo::Bar's constants and going > from there but I got nowhere. > > Any ideas? You were on the right track: module Foo class D; end class Bar class C1; end class C2; end K = 3 end end p Foo::Bar.constants.map{|k|Foo::Bar.const_get(k)}.grep(Class)