开发者

autoload with namespaces/submodules

开发者 https://www.devze.com 2023-01-01 19:07 出处:网络
I\'m using modules as namespaces in ruby. How would I go about autoloading...something like autoload :\"App::ModuleA\", \'app/module_a that doesn\'t throw a \"mus开发者_Go百科t be constant name\" erro

I'm using modules as namespaces in ruby. How would I go about autoloading...something like autoload :"App::ModuleA", 'app/module_a that doesn't throw a "mus开发者_Go百科t be constant name" error?


You need to pass a symbol to autoload (probably a typo in your question), and call it on the parent of the constant, like:

App.autoload :ModuleA, "app/module_a"

Note that this works for nested levels too. 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 attempt to access ModuleA, succeed by autoloading it, and only then attempt Inner, which succeeds also because it now knows where to autoload it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号