moduleUtilsclassStringUtilsdefself.testputs"test method myfile="+__FILE__+';load from '+$0endendendif__FILE__==$0puts'called from direct running'Utils::StringUtils.test()end
直接运行,结果,if条件成立,执行了输出
lineos:false
123
20:04:37-androidyue~/rubydir/test$ ruby utils.rb
called from direct running
test method myfile=utils.rb;load from utils.rb
引用Utils的类test.rb
lineos:false
12
require'./utils'Utils::StringUtils.test()
运行结果,引入模块的条件不成立,没有输出called from direct running
lineos:false
12
20:08:07-androidyue~/rubydir/test$ ruby test.rb
test method myfile=/home/androidyue/rubydir/test/utils.rb;load from test.rb