Forum: Ruby is there a way to get or list all available classes?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8d6f5daee16e380ce0ac00395b417fb6?d=identicon&s=25 uval (Guest)
on 2005-11-30 23:56
(Received via mailing list)
Hello,

topic says it all
for example irb knows many classes like Dir, File, Process, Thread ..
can I somehow list them, maybe the are kept in some array ..
if it's possible, can additional classes provided through
require "foo"
be listet too?

I hope my question is clear
thanks in advance

Regards, Daniel
D83785463666ae9bb98a0753eebc8950?d=identicon&s=25 nightphotos (Guest)
on 2005-12-01 00:04
(Received via mailing list)
Hi Daniel,

Try this:

C:\>irb
irb(main):001:0> ObjectSpace.each_object(Class) {|c| p c}
Gem::SourceIndex
OpenSSL::Digest::SHA1
OpenSSL::Digest::SHA
Gem::ConsoleUI
OpenSSL::Digest::RIPEMD160
Gem::StreamUI
...etc...etc...etc


Wayne Vucenic
No Bugs Software
Ruby and C++ Agile Contract Programming in Silicon Valley
37a3c73ffbf864e4b28f7f2384ee12ce?d=identicon&s=25 Timothy Hunter (tim-hunter)
on 2005-12-01 00:52
(Received via mailing list)
Daniel Schüle wrote:
> thanks in advance
>
> Regards, Daniel
>
classes = []
ObjectSpace.each_object {|obj| classes << obj if obj.class == Class}
classes = classes.sort_by {|cls| cls.inspect}
puts classes
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 bob.news (Guest)
on 2005-12-01 11:11
(Received via mailing list)
Wayne Vucenic wrote:
> OpenSSL::Digest::RIPEMD160
> Gem::StreamUI
> ..etc...etc...etc

Note, that all approaches using ObjectSpace list only classes known to
the
interpreter at this moment.  There might be more classes whose files
haven't been required / loaded yet.  You won't see them.  Also,
additional
classes may be generated dynamically.

Kind regards

    robert
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 david (Guest)
on 2005-12-03 10:47
(Received via mailing list)
A slightly more hackish solution I think that might also work with
autoloads (way too early in the morning to go and code it), is to use
#constants, eval those, select the ones that are classes, recurse,
rinse, repeat. Mind you, this is potentially a much worse resource hog
than the ObjectSpace approach.

David Vallner
97e3d8ba68b082414cc3438d15c9d6e2?d=identicon&s=25 robert.evans (Guest)
on 2005-12-05 00:08
(Received via mailing list)
Hi,

Not sure exactly which list of "classes available" you want, but if
you wanted to know all the classes available to the interpreter to
load, then you could conceivably iterate the directories in $:, the
load path, and look at all .rb files in them and their subdirectories
and parse them for the classes that they define.

Bob


On Nov 30, 2005, at 2:52 PM, Daniel Schüle wrote:
9dfe8c734b0f9b37a4e218425c0a2138?d=identicon&s=25 gene.tani (Guest)
on 2005-12-05 01:48
(Received via mailing list)
Robert Klemme wrote:
> > Gem::ConsoleUI
>
>     robert

right, and immediate values are kind of important:Fixnum, Symbol,
TrueClass, FalseClass, NilClass ;-p}
This topic is locked and can not be replied to.