我正在尝试使用ruby(2.4.1p111)和load方法,它确实有一些奇怪的行为:
我有两个文件:
mytest.rb :
class MyClass
def self.greet(param)
puts "Got called: #{param}"
end
greet 'Called locally'
load "./testld.rb"
end
和加载的文件:
testld.rb :
greet 'Called by load'
我从文档中了解到,来自 testld.rb 的加载代码和 MyClass 中的方法调用权限应该相同。相反,我得到了:
-bash-4.2$ ruby mytest.rb
Got called: Called locally
/Blacksmith/RB/testld.rb:1:in `<top (required)>': undefined method `greet'
for main:Object (NoMethodError)
from mytest.rb:9:in `load'
from mytest.rb:9:in `<class:MyClass>'
from mytest.rb:1:in `<main>'
知道我做错了吗?