class method self.autoloaders

Ruby on Rails edge

Since v6.0.6.1

Available in: v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3.2 v8.0.5.1 v8.1.3.1 edge

Signature

self.autoloaders()

Provides access to the application autoloaders.

The autoloader that manages autoload_paths is reachable as

Rails.autoloaders.main

This autoloader manages the constants that are reloaded when reloading is enabled.

The autoloader that manages autoload_once_paths is reachable as

Rails.autoloaders.once

This autoloader manages constants that are autoloaded, but not reloaded.

You can use these objects to customize their behavior, defining custom root namespaces, collapsing directories, configuring callbacks, etc.

# config/environments/development.rb
Rails.autoloaders.main.on_load("MyGateway") do
  MyGateway.endpoint = "https://my-gateway.localhost"
end

# config/environments/production.rb
Rails.autoloaders.main.on_load("MyGateway") do
  MyGateway.endpoint = "https://my-gateway.example.com"
end

The each iterator allows you to iterate over both loaders:

Rails.autoloaders.each do |loader|
  loader.log!
end

which may be handy if you want to run the same code for both of them.

Indeed, there is a shortcut for that common use case:

Rails.autoloaders.log!

which is handy to watch the activity of the autoloaders.

Finally, zeitwerk_enabled? allows you to check if autoloading is powered by Zeitwerk. This predicate returns a hard-coded true since Rails 7, but it is still in place for engines that support Rails 6.

The autoloaders are available really early, you can access them in the application class body, environment configuration, initializers, etc.

Please check the Autoloading and Reloading Constants guide and the documentation of Zeitwerk itself for more details and usage patterns.

Source
# File railties/lib/rails.rb, line 186
    def autoloaders
      application.autoloaders
    end

Defined in railties/lib/rails.rb line 186 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Rails

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close