Skip to main content

autoload_with_const_missing

🔄 Leveraging autoload with const_missing​

Combine Module#autoload with a custom const_missing hook to lazily load or generate constants on demand. This pattern helps keep your startup time low while ensuring only the necessary constants are loaded. You can also implement fallbacks if the file path doesn’t exist.

module MyApp
# Automatically load `MyApp::ServiceX` from "my_app/service_x.rb"
autoload :ServiceX, "my_app/service_x"

def self.const_missing(name)
warn "Constant \\#{name} not found, attempting dynamic generation..."
# e.g., generate a null object or proxy
const_set(name, Class.new do
def method_missing(m, *args) ; nil ; end
end)
end
end

# Usage
MyApp::ServiceX.new.perform
MyApp::NonExistentConstant.some_method # Triggers const_missing