Usually there are lib, bin and spec(or test) folders in the ruby project(at least, in the generated gem). Now there are many require_relative imports in each file in the lib and also ugly require_relative ‘../lib/some_files’ in the spec(or test) file. Is there any way to eliminate all those require_relative imports? I think there’s some kind of trick with FILE but I don’t know which exactly.
Usually there are lib, bin and spec(or test) folders in the ruby project(at least,
Share
You can do something like this:
This will require all files in the subfolder of current directory. You can use require_relative as well because it is Kernel function, nothing special.
But if these files depend on each other and must be loaded in the defined order then you will have to manually specify
require/require_relativein the defined order.Also if you need to eliminate require duplication in spec and lib, then you can go this way: add file with the name of top-level namespace to the root of your
/libfolder, require nested files from here and then require this single file from/spec/spec_helper.rb:This is simplified example and you can have such initialization files for each nested module etc.