Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Kernel#autoload
(::interned const, ::path filename) -> nil
autoload(const, filename) → nil
load.c
View on GitHub ↗
static VALUE
rb_f_autoload(VALUE obj, VALUE sym, VALUE file)
{
VALUE klass = rb_class_real(rb_vm_cbase());
if (!klass) {
rb_raise(rb_eTypeError, "Can not set autoload on singleton class");
}
return rb_mod_autoload(klass, sym, file);
}
Registers filename to be loaded (using Kernel::require) the first time that const (which may be a String or a symbol) is accessed.
Example
autoload(:MyModule, "/usr/local/lib/modules/my_module.rb")
If const is defined as autoload, the file name to be loaded is replaced with filename. If const is defined but not as autoload, does nothing.
Files that are currently being loaded must not be registered for autoload.