Module#autoload
This method registers a file path for the given class or module and lazily loads the module the first time it is accessed.
For Example:
Now let’s run person.rb
Module#autoload?
This method returns the registered file path if the module is not loaded
and nil
if the module has already been loaded.
From the above example, we can see that first autoload?
call returns the registered file path.
Calling Profile
will load the module.
Thus the second autoload?
call returns nil
because the module is already loaded.
Now, let’s take an example of inheritance.
autoload?
call from Manager
class returns the file path of Profile
as it is defined in the parent class.
autoload
directives are inherited from parent classes.
But there is no way to check whether autoload
is defined in the child class or not.
Ruby 2.7 has added
an optional argument inherit
to autoload?
similar to Module#const_defined?
.
The value of inherit
can be true
or false
with the default being true
.
The new method signature is autoload?(CONSTANT_NAME, inherit = true)
.
First call of autoload?
returns the file path because autoload is defined in the parent class.
Second call returns nil
because we have passed option inherit
as false
so it doesn’t check the constant in ancestors.