Understanding load path in Ruby -
i working library has lot of ruby files generated protocol buffers ruby.
the example of require path follows file in /path/to/source_folder/lib_name/level_1/level_2/level_3
require 'lib_name/level_1/level_2/level_3/file_name_1' require 'lib_name/level_1/level_2/level_3/file_name_2'
all necessary files , folders contained inside source_folder
location. had example been in c++ can run using
g++ file_name.cpp -i "/path/to/source_folder"
i cannot change every require require_relative
, make changes main code cumbersome. trying find simple way run files.
i have 2 solution.
% tree . . ├── lib │ └── dir1 │ └── dir2 │ ├── a.rb │ └── b.rb └── main.rb % cat a.rb def method_a puts :execute_a end % cat b.rb def method_b puts :execute_b end
no.1 add load path sub directories
def add_load_path_recursive(dir) dir.glob(dir + "/**/*/").each |dir| $load_path << file.expand_path(dir) end end add_load_path_recursive "./lib" require 'a' require 'b' method_a method_b
no.2 require 'rb' files (recursive)
def require_recursive(dir) dir.glob(dir + "/**/*.rb").each |dir| require file.expand_path(dir) end end require_recursive "./lib" method_a method_b
Comments
Post a Comment