How to get an Array of all constants of ruby core lib?

Hi,

I apply .instance_methods(false) on class String, class Array etc…

I build up an Array for completable methods, and feed this to the
ruby Readline module. This already works fine as is, completion works.

But there is one problem:

Right now I hardcode this. So I have an Array that I have to maintain
on my own and I perhaps missed some core components (I am sloppy and
lazy)

So here is my question:

  • Is there a way to programmatically obtain all corelib constants?
    (In particular, I am interested in the major classes/modules that
    are part of corelib such as class Hash and so forth, not any sublevel
    constants.)

So that I get an Array such as:

[ String, Float, Object, Hash, Array etc…]

To create an array with separate objects a block can be passed instead.
This method is … In case of Array’s each, all elements in the Array
instance are yielded to the supplied block in sequence.

http://www.pradhanmantriyojana.com/2016/01/12/pradhan-mantri-suraksha-bima-yojana/

Robert H. wrote in post #1180462:

  • Is there a way to programmatically obtain all corelib constants?
    (In particular, I am interested in the major classes/modules that
    are part of corelib such as class Hash and so forth, not any sublevel
    constants.)

So that I get an Array such as:

[ String, Float, Object, Hash, Array etc…]

get all known class :

def getc()
h={}
ObjectSpace.each_object {|a| h[a.name]=a if a && Class === a }
h
end

get all constants of all class:

def getconst()
h={}
ObjectSpace.each_object do |a|
h[a.name]=a.constants if a && Class === a && a.constants.size>0
end
h
end

All constants which are not Class or Module :

def get_data_const()
h={}
ObjectSpace.each_object do |a|
if a && Class === a && a.constants.size>0
l=a.constants.select {|const|
v=a.const_get(const);
v.class!=Class && v.class!=Module
}
h[a.name]= l if l.size>0
end
end
h
end

l= get_data_const()

require ‘pp’
pp l

{“Complex”=>[:I],
“ThreadGroup”=>[:Default],
“Thread”=>[:MUTEX_FOR_THREAD_EXCLUSIVE],
“RubyVM”=>[:OPTS, :INSTRUCTION_NAMES, :DEFAULT_PARAMS],
“Random”=>[:DEFAULT],
“File”=>
[:Separator,
:SEPARATOR,
:FNM_CASEFOLD,
:FNM_EXTGLOB,
:FNM_SYSCASE],
“IO”=>
[:SEEK_SET,
:SEEK_CUR,
:LOCK_NB,

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs