C:/Users/Lenovo/rubycode/test1 下有两个文件:max.rb 和 a.rb
# 情况1
# Code in max.rb:
puts __FILE__
puts $0
require "./a"
puts __FILE__
puts $0
# Code in a.rb
puts __FILE__
puts $0
C:/Users/Lenovo/rubycode/test1 ruby max.rb
# 输出如下:
test1/max.rb
test1/max.rb
C:/Users/Lenovo/rubycode/test1/a.rb
test1/max.rb
test1/max.rb
test1/max.rb
# 但是,若
C:/Users/Lenovo/rubycode ruby test1/max.rb
# 则会输出错误
test1/max.rb
test1/max.rb
Traceback (most recent call last):
2: from test1/max.rb:5:in `<main>'
1: from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- ./a (LoadError)
# 情况2
# Code in max.rb:
puts __FILE__
puts $0
require_relative "a"
puts __FILE__
puts $0
# Code in a.rb
puts __FILE__
puts $0
C:/Users/Lenovo/rubycode/test1 ruby max.rb 及 C:/Users/Lenovo/rubycode ruby test1/max.rb
# 输出皆如下:
test1/max.rb
test1/max.rb
C:/Users/Lenovo/rubycode/test1/a.rb
test1/max.rb
test1/max.rb
test1/max.rb