January 2024
Intermediate to advanced
718 pages
20h 15m
English
Knowing about objects is one aspect of reflection, but to get the whole picture, you also need to be able to look at classes—and 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 and its children using Class#subclasses. For classes and modules, the Module#ancestors method lists both superclasses and mixed-in modules:
| | klass = Integer |
| | print "Inheritance chain: " |
| | begin |
| | print klass |
| | klass = klass.superclass |
| | print " < " if klass |
| | end while klass |
| | puts |
| | p "Ancestors: #{Integer.ancestors}" |
| | p "Subclasses: #{Integer.subclasses}" |
Produces:
| | Inheritance chain: Integer < ... |
Read now
Unlock full access