Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 检测当前文件是否为主文件_Ruby_File_Recursion - Fatal编程技术网

Ruby 检测当前文件是否为主文件

Ruby 检测当前文件是否为主文件,ruby,file,recursion,Ruby,File,Recursion,如何检测文件的当前读取是直接从命令行完成的,还是通过加载/请求完成的 有一个众所周知的方法: __FILE__ == $0 但是,当主文件可能自行加载时,这种方法无法正常工作。这太容易了。我将只分配一个标志并检查它。一件稍微棘手的事情是相关作业的顺序。(a) 条件,(b)指定标志,以及(c)装载必须按照以下顺序进行: if @loaded and __FILE__ == $0 # the main reading ... else # not the main reading

如何检测文件的当前读取是直接从命令行完成的,还是通过加载/请求完成的

有一个众所周知的方法:

__FILE__ == $0

但是,当主文件可能自行加载时,这种方法无法正常工作。

这太容易了。我将只分配一个标志并检查它。一件稍微棘手的事情是相关作业的顺序。(a) 条件,(b)指定标志,以及(c)装载必须按照以下顺序进行:

if @loaded and __FILE__ == $0
  # the main reading
  ...
else
  # not the main reading
  ...
end
...
@loaded = true
...
the_relevant_command_that_leads_to_loading_of_a_file

如果我想让它们按不同的顺序排列,我必须嵌入其中一些并强制执行此顺序。

你能举一个“主文件加载本身”的例子吗?