`if __FILE__ == $0` 在 Ruby 中是什么意思 [英] What does `if __FILE__ == $0` mean in Ruby
本文介绍了`if __FILE__ == $0` 在 Ruby 中是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
if __FILE__ == $0
$:.unshift File.join(File.dirname(__FILE__),'..')
我在 Ruby 中找到了这段代码,是什么意思?
I found this code in Ruby, what's the meaning?
推荐答案
# Only run the following code when this file is the main file being run
# instead of having been required or loaded by another file
if __FILE__==$0
# Find the parent directory of this file and add it to the front
# of the list of locations to look in when using require
$:.unshift File.expand_path("../../", __FILE__)
end
请注意,随着 require_relative
的出现,通常不需要这种特殊做法(添加到 LOAD_PATH 目录),并且 expand_path
是一种更简单的查找父目录的方法.
Note that this particular practice (adding to the LOAD_PATH directory) is not usually necessary with the advent of require_relative
, and expand_path
is a simpler way of finding the parent directory.
这篇关于`if __FILE__ == $0` 在 Ruby 中是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文