[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to get the list of all classes?

Tobias Reif

9/4/2003 7:36:00 PM

Hi

I'm writing a syntax highlighting program [code] for Ruby code listings
in XML (eg DocBook) documents.

To get the global variables I do
puts Kernel.global_variables
and to get the names of some common methods I do
puts methods
and
puts Kernel.methods
8 Answers

Wesley J. Landaker

9/4/2003 7:41:00 PM

0

Apparently, Tobias Reif recently wrote:
> Hi
>
> I''m writing a syntax highlighting program [code] for Ruby code listings
> in XML (eg DocBook) documents.
>
> To get the global variables I do
> puts Kernel.global_variables
> and to get the names of some common methods I do
> puts methods
> and
> puts Kernel.methods
> .
>
> How to get the names of all classes? (all built-in classes or all
> available)
> I need to get "File", "Hash", etc.

Here is one way:

$ ruby -e ''x = []; ObjectSpace.each_object { |o| x << o if o.class ==
Class }; p x''
[UnboundMethod, Method, Binding, Proc, SystemStackError, LocalJumpError,
Struct::Tms, Process::Status, Time, Dir, File::Stat, File, IO, EOFError,
IOError, Range, MatchData, Regexp, RegexpError, Struct, Hash, Array,
Bignum, Float, Fixnum, Integer, Numeric, FloatDomainError,
ZeroDivisionError, ThreadGroup, Continuation, Thread, ThreadError,
Errno::EDQUOT, Errno::EREMOTEIO, Errno::EISNAM, Errno::ENAVAIL,
Errno::ENOTNAM, Errno::EUCLEAN, Errno::ESTALE, Errno::EINPROGRESS,
Errno::EALREADY, Errno::EHOSTUNREACH, Errno::EHOSTDOWN,
Errno::ECONNREFUSED, Errno::ETIMEDOUT, Errno::ETOOMANYREFS,
Errno::ESHUTDOWN, Errno::ENOTCONN, Errno::EISCONN, Errno::ENOBUFS,
Errno::ECONNRESET, Errno::ECONNABORTED, Errno::ENETRESET,
Errno::ENETUNREACH, Errno::ENETDOWN, Errno::EADDRNOTAVAIL,
Errno::EADDRINUSE, Errno::EAFNOSUPPORT, Errno::EPFNOSUPPORT,
Errno::EOPNOTSUPP, Errno::ESOCKTNOSUPPORT, Errno::EPROTONOSUPPORT,
Errno::ENOPROTOOPT, Errno::EPROTOTYPE, Errno::EMSGSIZE,
Errno::EDESTADDRREQ, Errno::ENOTSOCK, Errno::EUSERS, Errno::ESTRPIPE,
Errno::ERESTART, Errno::EILSEQ, Errno::ELIBEXEC, Errno::ELIBMAX,
Errno::ELIBSCN, Errno::ELIBBAD, Errno::ELIBACC, Errno::EREMCHG,
Errno::EBADFD, Errno::ENOTUNIQ, Errno::EOVERFLOW, Errno::EBADMSG,
Errno::EDOTDOT, Errno::EMULTIHOP, Errno::EPROTO, Errno::ECOMM,
Errno::ESRMNT, Errno::EADV, Errno::ENOLINK, Errno::EREMOTE, Errno::ENOPKG,
Errno::ENONET, Errno::ENOSR, Errno::ETIME, Errno::ENODATA, Errno::ENOSTR,
Errno::EBFONT, Errno::EBADSLT, Errno::EBADRQC, Errno::ENOANO,
Errno::EXFULL, Errno::EBADR, Errno::EBADE, Errno::EL2HLT, Errno::ENOCSI,
Errno::EUNATCH, Errno::ELNRNG, Errno::EL3RST, Errno::EL3HLT,
Errno::EL2NSYNC, Errno::ECHRNG, Errno::EIDRM, Errno::ENOMSG, Errno::ELOOP,
Errno::ENOTEMPTY, Errno::ENOSYS, Errno::ENOLCK, Errno::ENAMETOOLONG,
Errno::EDEADLK, Errno::ERANGE, Errno::EDOM, Errno::EPIPE, Errno::EMLINK,
Errno::EROFS, Errno::ESPIPE, Errno::ENOSPC, Errno::EFBIG, Errno::ETXTBSY,
Errno::ENOTTY, Errno::EMFILE, Errno::ENFILE, Errno::EINVAL, Errno::EISDIR,
Errno::ENOTDIR, Errno::ENODEV, Errno::EXDEV, Errno::EEXIST, Errno::EBUSY,
Errno::ENOTBLK, Errno::EFAULT, Errno::EACCES, Errno::ENOMEM,
Errno::EAGAIN, Errno::ECHILD, Errno::EBADF, Errno::ENOEXEC, Errno::E2BIG,
Errno::ENXIO, Errno::EIO, Errno::EINTR, Errno::ESRCH, Errno::ENOENT,
Errno::EPERM, SystemCallError, NoMemoryError, SecurityError, RuntimeError,
NotImplementedError, LoadError, SyntaxError, ScriptError, NoMethodError,
NameError, RangeError, IndexError, ArgumentError, TypeError,
StandardError, Interrupt, SignalException, fatal, SystemExit, Exception,
String, FalseClass, TrueClass, Data, Symbol, NilClass, Class, Module,
Object]



Ryan Pavlik

9/4/2003 8:02:00 PM

0

On Fri, 5 Sep 2003 04:41:13 +0900
"Wesley J. Landaker" <wjl@icecavern.net> wrote:

<snip>
> Here is one way:
>
> $ ruby -e ''x = []; ObjectSpace.each_object { |o| x << o if o.class ==
> Class }; p x''
<snip>

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.

--
Ryan Pavlik <rpav@mephle.com>

"So... I''m not a total bad-ass fueled by relentless evil?" - 8BT

Mark J. Reed

9/4/2003 8:38:00 PM

0

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''
> <snip>
>
> 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.

-Mark

Wesley J. Landaker

9/4/2003 8:58:00 PM

0

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''
>> <snip>
>>
>> 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

Jason Creighton

9/5/2003 1:07:00 AM

0

On Thu, 04 Sep 2003 20:37:32 GMT
"Mark J. Reed" <markjreed@mail.com> 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''
> > <snip>
> >
> > 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:

Yes, that''s seemed odd to me at times as well.

> module ObjectSpace
> class << self
> include Enumerable
> def each(*args, &block) each_object(*args, &block) end
> end
> end

err....why don''t you just alias each to each_object?

module ObjectSpace
class << self
include Enumerable
alias :each :each_object
end
end

No mess, no fuss, no greasy aftertaste.

> 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.

It only seems backwards if you''re used to Ruby. :)

Jason Creighton

Mark J. Reed

9/5/2003 12:53:00 PM

0

MJR = me
JC = Jason Creighton


MJR> module ObjectSpace
MJR> class << self
MJR> include Enumerable
MJR> def each(*args, &block) each_object(*args, &block) end
MJR> end
MJR> end

JC> err....why don''t you just alias each to each_object?

JC> module ObjectSpace
JC> class << self
JC> include Enumerable
JC> alias :each :each_object
JC> end
JC> end

Uhm . . . because it''s too easy?
I wanted to show off my understanding of passing &blocks around?
Maybe I''m a firm believer in TMTOWTDI?
Would you believe . . . I just forgot about alias? :)

Thanks.

-Mark

Tobias Reif

9/5/2003 9:39:00 PM

0

How to get the list of names which includes "print"?

TIA,
Tobi

I wrote:
> I''m writing a syntax highlighting program [code] for Ruby code
> listings in XML (eg DocBook) documents.
[...]
> Any other wordlist-returning snippets are welcome too.

--
http://www.pink...





Tobias Reif

9/5/2003 9:43:00 PM

0

Sorry, please ignore the previous post.
(forgot to look in Kernel.methods)

Tobi

> How to get the list of names which includes "print"?
[...]

--
http://www.pink...