Knowing about objects is one part of reflection, but to get the whole
picture, you also need to be able to look at classes---the methods
and constants that they contain.
Looking at the class hierarchy is easy. You can get the parent of any
particular class using
Class#superclass
. For
classes
and modules,
Module#ancestors
lists both
superclasses and mixed-in modules.
klass = Fixnum
begin
print klass
klass = klass.superclass
print " < " if klass
end while klass
puts
p Fixnum.ancestors
|
produces:
Fixnum < Integer < Numeric < Object
[Fixnum, Integer, Precision, Numeric, Comparable, Object, Kernel]
|
If you want to build a complete class hierarchy, just run that code
for every class in the system. We can use
ObjectSpace
to iterate
over all
Class
objects:
ObjectSpace.each_object(Class) do |aClass|
# ...
end
|