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

D

Daniel Schüle

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
 
T

Timothy Hunter

Daniel said:
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
classes = []
ObjectSpace.each_object {|obj| classes << obj if obj.class == Class}
classes = classes.sort_by {|cls| cls.inspect}
puts classes
 
W

Wayne Vucenic

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
 
R

Robert Klemme

Wayne said:
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

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
 
D

David Vallner

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
 
R

Robert Evans

Hi,

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

and parse them for the classes that they define.

Bob
 
G

Gene Tani

Robert said:
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

right, and immediate values are kind of important:Fixnum, Symbol,
TrueClass, FalseClass, NilClass ;-p}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top