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

Discussion in 'Ruby' started by Daniel Schüle, Nov 30, 2005.

  1. 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
    Daniel Schüle, Nov 30, 2005
    #1
    1. Advertising

  2. Daniel Schüle wrote:
    > 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
    Timothy Hunter, Nov 30, 2005
    #2
    1. Advertising

  3. Re: is there a way to get or list all available classes?

    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


    On 11/30/05, Daniel Sch=FCle <-karlsruhe.de> wrote:
    > 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
    >
    >
    >
    Wayne Vucenic, Nov 30, 2005
    #3
  4. Re: is there a way to get or list all available classes?

    Wayne Vucenic wrote:
    > 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
    Robert Klemme, Dec 1, 2005
    #4
  5. 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
    David Vallner, Dec 3, 2005
    #5
  6. Daniel Schüle

    Robert Evans Guest

    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


    On Nov 30, 2005, at 2:52 PM, Daniel Sch=FCle wrote:

    > 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
    >
    >
    Robert Evans, Dec 4, 2005
    #6
  7. Daniel Schüle

    Gene Tani Guest

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

    Robert Klemme wrote:
    > Wayne Vucenic wrote:
    > > 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


    right, and immediate values are kind of important:Fixnum, Symbol,
    TrueClass, FalseClass, NilClass ;-p}
    Gene Tani, Dec 5, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carfield Yim
    Replies:
    1
    Views:
    1,397
    Andrew Thompson
    May 31, 2004
  2. aeonsun
    Replies:
    4
    Views:
    562
    Paul Cager
    Jul 27, 2006
  3. Anjan Bhowmik
    Replies:
    1
    Views:
    462
    Misbah Arefin
    Feb 14, 2008
  4. Michael Winterstein
    Replies:
    2
    Views:
    137
    burke
    Jan 26, 2010
Loading...

Share This Page