How to get the list of all classes?

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.

Any other wordlist-returning snippets are welcome too.

TIA,
Tobi

[code]
XSLT:
http://www.pinkjuice.com/howto/vimxml/xslt/tinydbk2xhtml/markup_ruby.xslt
sample:
http://www.pinkjuice.com/howto/vimxml/setup.xml#jing
(scroll down)

···


http://www.pinkjuice.com/

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]

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.pinkjuice.com/

> Here is one way: > > $ ruby -e 'x = []; ObjectSpace.each_object { |o| x < 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.

···

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


Ryan Pavlik rpav@mephle.com

“So… I’m not a total bad-ass fueled by relentless evil?” - 8BT

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.pinkjuice.com/

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

···

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.

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

$ 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:

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

Jason Creighton

···

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:

MJR = me
JC = Jason Creighton

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

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? :slight_smile:

Thanks.

-Mark

Hi –

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

Or even:

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

Or, wandering into arguably too-compressed territory:

class << ObjectSpace.extend(Enumerable)
alias :each :each_object
end

David

···

On Fri, 5 Sep 2003, Jason Creighton wrote:

On Thu, 04 Sep 2003 20:37:32 GMT > “Mark J. Reed” markjreed@mail.com wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

— “Mark J. Reed” markjreed@mail.com wrote: > MJR = me

JC = Jason Creighton

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

Uhm . . . because it’s too easy?

I agree with Jason here, alias is the way to go, it even speeds it up on
my machine, odd as it seems.

I wanted to show off my understanding of passing &blocks around?
Maybe I’m a firm believer in TMTOWTDI?

TMTOWTDI is a perl idiom :slight_smile:

Would you believe . . . I just forgot about alias? :slight_smile:

It happens :slight_smile: I have aliases too: people call me “percy” from time to
time, depending on who’s asking :slight_smile:

– Thomas Adam

···

=====
Thomas Adam

“The Linux Weekend Mechanic” – www.linuxgazette.com


Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger http://mail.messenger.yahoo.co.uk