How can I list all the types that are declared by a module in Ruby?
A Ruby module is nothing more than a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules. Modules have two uses. You can use a module as a convenient way to bundle objects together, or you can incorporate its contents into a class with Ruby's include statement.
To access the instance method defined inside the module, the user has to include the module inside a class and then use the class instance to access that method.
As with class methods, you call a module method by preceding its name with the module's name and a period, and you reference a constant using the module name and two colons.
Modules are collections of methods and constants. They cannot generate instances. Classes may generate instances (objects), and have per-instance state (instance variables). Modules may be mixed in to classes and other modules.
Use the constants method defined in the Module module. From the Ruby documentation:
Module.constants => array
Returns an array of the names of all constants defined in the system. This list includes the names of all modules and classes.
p Module.constants.sort[1..5]
produces:
["ARGV", "ArgumentError", "Array", "Bignum", "Binding"]
You can call constants on any module or class you would like.
p Class.constants
Not sure if this is what you mean, but you can grab an array of the names of all constants and classes defined in a module by doing
ModuleName.constants
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With