From: Ben Bleything Date: 2007-03-06T09:28:47+09:00 Subject: Re: nifty one-liner to search ObjectSpace? On Tue, Mar 06, 2007, Giles Bowkett wrote: > Say I've got a class and I know it has subclasses. I'm absolutely sure > there must be an irb ObjectSpace one-liner to give me all the > subclasses of an arbitrary class. Yup :) > I've been able to find the Class instance method #inherited(), which > takes another class and tells you yay or nay, but a simple one-liner > to return a class's subclasses has so far proved out of my reach. ObjectSpace.each_object( Class ) {|klass| puts klass if klass < IO } ...will give you any class that is a subclass of IO. Could pretty easily be wrapped into a method you could call on a class. In fact: class Class def subclasses out = [] ObjectSpace.each_object( Class ) {|k| out << k if k < self } return out end end Stick that in your .irbrc and call IO.subclasses. YEAH. > I know it exists. I just don't know what it is. I'm also kind of > stymied because it appears ObjectSpace has #each_object but not #each. > That seems so weird to me. What would #each do that #each_object doesn't? Ben