Startup with namespaces / submodules

I use modules as namespaces in ruby. How can I autoload :"App::ModuleA", 'app/module_a ... something like autoload :"App::ModuleA", 'app/module_a , which does not cause an error "should be a permanent name"?

+7
ruby autoload
source share
1 answer

You need to pass the autoload symbol (perhaps a typo in your question) and call it the constant's parent, for example:

 App.autoload :ModuleA, "app/module_a" 

Note that this also works for nested levels. Let's say that in app/module_a you have:

 module App::ModuleA autoload :Inner, "path/to/inner" end 

When Ruby encounters App::ModuleA::Inner , it will first try to access ModuleA , succeed at startup, and only then try to execute Inner , which will succeed, since it now knows where its startup is.

+11
source share

All Articles