如果我想require在Ruby中使用相关文件并且希望它在1.8.x和> = 1.9.2中工作,那么最佳实践是什么?
我看到几个选项:
$LOAD_PATH << '.'忘记一切$LOAD_PATH << File.dirname(__FILE__)require './path/to/file'RUBY_VERSION<1.9.2,然后定义require_relative为require,require_relative随后在需要的地方使用require_relative已存在,如果已存在,请尝试按上一种情况继续操作require File.join(File.dirname(__FILE__), 'path/to/file')
Run Code Online (Sandbox Code Playgroud)它们似乎不能在Ruby 1.9中工作,因为,例如:$ cat caller.rb
require File.join(File.dirname(__FILE__), 'path/to/file')
$ cat path/to/file.rb
puts 'Some testing'
$ ruby caller
Some testing
$ pwd
/tmp
$ ruby /tmp/caller
Some testing
$ ruby tmp/caller
tmp/caller.rb:1:in 'require': no such file to load -- tmp/path/to/file (LoadError)
from tmp/caller.rb:1:in '<main>' …Run Code Online (Sandbox Code Playgroud)