fxn/zeitwerk

Convention on using loaders?

faraaz-deepsource opened this issue · 3 comments

Is there a convention on how a file creating and executing the loaders should be required?

for example I have an initializer.rb like this:

require 'zeitwerk'  

loader = Zeitwerk::Loader.new
loader.push_dir('config')
loader.setup
loader.eager_load

that adds the dirs in the loader and calls eager_load. this initializer.rb just gets require_relatived and the autoloading is done as a "side effect".

Is there a convention on writing a file that declares loaders like this or should I put it in a class and call Initializer::initialize()?

its a non-rails, non-gem project

fxn commented

Not really, and that setup looks clear and good to me.

The only potential caveat I see is that the argument "config" is relative, which means relative to the CWD. This is fine if you control the CWD, but otherwise an absolute path is the way to go. For example:

loader.push_dir("#{__dir__}/config")

Understood. Thank you have a nice day!