1 Get the list of defined entities
Ruby's Reflection API enables us to detect classes and objects at runtime. So we'll introduce some of the methods of the definitions in module, Class, and object below.
module modules have a constants method that returns all the constant names in the system, including the class name and module name. The nesting method returns a list of nested modules on the current call point.
Ruby Code
list = math.constants # ["E", "PI"]
Module#ancestors returns all the contained classes or modules of the specified class or module.
Ruby Code
List = array.ancestors
# [Array, Enumerable, Object, Kernel]
The Class_variables method returns a table of all the class variables for the given class and his superclass. The Included_modules method lists all the modules that are included in this class.
Ruby Code
Class Parent
@ @var1 = nil
end
class Child < Parent
@ @var2 = nil
end
list1 = Parent.class_variables # ["@ @var1"]
list2 = array.included_modules # [enumerable, Kernel]