Reputation: 760
I am working with a library which has a lot of ruby files generated by protocol Buffers for ruby.
The example of a require path is as follows for a 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 the necessary files and folders are contained inside a source_folder
with some location. Had this example been in C++ then we can run this using
g++ file_name.cpp -I "/path/to/source_folder"
I cannot really change every require to require_relative
and make changes to the main code as it will be too cumbersome. I am trying to find a simple way to run my files.
Upvotes: 0
Views: 107
Reputation: 1161
I have two 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 all sub directories
def add_load_path_recursive(dir)
Dir.glob(dir + "/**/*/").each do |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 all 'rb' files (recursive)
def require_recursive(dir)
Dir.glob(dir + "/**/*.rb").each do |dir|
require File.expand_path(dir)
end
end
require_recursive "./lib"
method_a
method_b
Upvotes: 1