Files in the same directory, such as/USR/LOCAL/RUBY/FOO.RB and/usr/local/ruby/bar.rb two files.
If you are directly in the FOO.RB
A bar.rb error cannot be found at execution time.
This is because the running
/home/oldsong$ ruby/usr/local/ruby/foo.rb
The BAR.RB is found in the Lib directory and the/home/oldsong/directory where Ruby is installed. Instead of looking under the directory/usr/local/ruby/of the RB file.
So in addition to referencing system RB, a relative path cannot be used in require.
Here's a couple of ways to refer to all RB in a single and directory context, combined with my personal experience.
1. Reference a file
Example: referencing the current RB file_to_require.rb under the same directory
Introduce 3 methods First
Require File.join (__file_, '. /file_to_require ').
require File.expand_path ('.. /file_to_require ', __file__)
require file.dirname (__file__) + '/file_to_require '
Among them, File.expand_path is the common practice of rails.
__FILE__ is a constant that represents the absolute path of the current file, such as/home/oldsong/test.rb
Law four:
$LOAD _path.unshift (File.dirname (__file__))
require ' bar '
First add the directory to the Load_path variable, and then refer to the file name directly.
2. Refer to all files in a directory
Ruby does not have the import java.io.* in Java;
You cannot use wildcard characters when referencing, and it is estimated that future versions may be added.
Example: Refer to all *.rb files under lib/files under the same directory as current RB.
Law I:
Dir[file.dirname (__file__) + '/lib/*.rb '].each {|file| require File}
Law II:
A gem to fix
Https://rubygems.org/gems/require_all