From: "Wesley J. Landaker" Date: 2003-09-05T05:57:50+09:00 Subject: Re: how to get the list of all classes? Apparently, Mark J. Reed recently wrote: > On Fri, Sep 05, 2003 at 05:01:50AM +0900, Ryan Pavlik wrote: >> > $ ruby -e 'x = []; ObjectSpace.each_object { |o| x << o if o.class == >> > Class }; p x' >> >> >> This is probably quicker if you do: >> >> x = [] >> ObjectSpace.each_object(Class) { |c| x << c } >> >> ..so you don't have to iterate through thousands of non-Class objects. > > It seems odd to me that ObjectSpace doesn't mixin Enumerable, but > that's easily remedied: > > module ObjectSpace > class << self > include Enumerable > def each(*args, &block) each_object(*args, &block) end > end > end > > Then you can just do this: > > ObjectSpace.find_all { |c| c.class == Class } > > Although it is still, as noted above, probably less efficient than > passing Class to each_object. But it's an aesthetic improvement, > I think. I mean, the idea of having to iterate over > a collection with a block that does nothing but an array append, just > to get the collection into array form, seems very backwards to me. I agree that having ObjectSpace enumerable would make sense. Actually, the first thing I tried when I saw the original message was: ObjectSpace.select { |x| x.class == Class } ... but then I realized that it wasn't Enumerable. Wes Wes