在ruby中__FILE__ == $PROGRAM_NAME是什么意思?(复制)

时间:2021-02-04 22:34:52

This question already has an answer here:

这个问题已经有了答案:

I have stumbled across this sintax while reviewing a code in Ruby. The code is:

我在查看Ruby中的代码时偶然发现了这个sintax。的代码是:

if __FILE__ == $PROGRAM_NAME
  #some code...
end

I suppose __FILE__ is a variable that gets me the name of the file I am in? But what does $PROGRAM_NAME simbolize then? Also, why is this if statement necessary, since the program works with or without it?

我想__FILE__是一个变量,它获取我所在文件的名称?但是什么是$PROGRAM_NAME sim玻利维亚化呢?另外,为什么这个if语句是必要的,因为程序使用或不使用它?

2 个解决方案

#1


5  

__FILE__ always returns the path of the source file. It's not a variable so you can't assign value to it. Whether it returns a relative path or an absolute one depends on how you run the script.

__FILE__总是返回源文件的路径。它不是变量,所以你不能给它赋值。它返回的是相对路径还是绝对路径取决于您如何运行脚本。

$PROGRAM_NAME or $0 by default returns the command that boots the program (minus the path of ruby interpreter). For example, you have a script file test.rb like this:

$PROGRAM_NAME或$0默认返回引导程序的命令(减去ruby解释器的路径)。例如,您有一个脚本文件测试。rb:

#!/usr/bin/env ruby
puts __FILE__
puts $PROGRAM_NAME

If you run this script with ruby test.rb, it prints

如果您使用ruby测试运行这个脚本。rb,它打印

test.rb
test.rb

If you run the script with ruby /path/to/test.rb, it prints

如果您使用ruby /path/to/test运行脚本。rb,它打印

/path/to/test.rb
/path/to/test.rb

If you give the script an execution permission and run it with ./test.rb, it prints

如果您给脚本一个执行权限并使用./test运行它。rb,它打印

./test.rb
./test.rb

Unlike __FILE__, $PROGRAM_NAME and $0 are real global variables, and you can change their values. $PROGRAM_NAME and $0 are aliases to each other, so you change the value of either one, the value of the other will change accordingly. For example, you have a test2.rb like this:

与__FILE__不同,$PROGRAM_NAME和$0是真正的全局变量,您可以更改它们的值。$PROGRAM_NAME和$0是彼此的别名,因此您可以更改其中一个的值,另一个的值将相应地更改。例如,您有一个test2。rb:

#!/usr/bin/env ruby
$0 = 'Hello, world!'
puts $0
puts $PROGRAM_NAME

it prints

它打印

Hello, world!
Hello, world!

#2


1  

__FILE__ is the current source file name.

__FILE__是当前的源文件名。

It seems to ruby code wants to make sure that the current file corresponds to the program that needs to executed.

ruby代码似乎希望确保当前文件对应于需要执行的程序。

$ before a variable, means its a global variable.

$在变量之前,意味着它是一个全局变量。

Check here to know more - http://www.zenspider.com/Languages/Ruby/QuickRef.html#18

点击这里了解更多信息——http://www.zenspider.com/Languages/Ruby/QuickRef.html#18

#1


5  

__FILE__ always returns the path of the source file. It's not a variable so you can't assign value to it. Whether it returns a relative path or an absolute one depends on how you run the script.

__FILE__总是返回源文件的路径。它不是变量,所以你不能给它赋值。它返回的是相对路径还是绝对路径取决于您如何运行脚本。

$PROGRAM_NAME or $0 by default returns the command that boots the program (minus the path of ruby interpreter). For example, you have a script file test.rb like this:

$PROGRAM_NAME或$0默认返回引导程序的命令(减去ruby解释器的路径)。例如,您有一个脚本文件测试。rb:

#!/usr/bin/env ruby
puts __FILE__
puts $PROGRAM_NAME

If you run this script with ruby test.rb, it prints

如果您使用ruby测试运行这个脚本。rb,它打印

test.rb
test.rb

If you run the script with ruby /path/to/test.rb, it prints

如果您使用ruby /path/to/test运行脚本。rb,它打印

/path/to/test.rb
/path/to/test.rb

If you give the script an execution permission and run it with ./test.rb, it prints

如果您给脚本一个执行权限并使用./test运行它。rb,它打印

./test.rb
./test.rb

Unlike __FILE__, $PROGRAM_NAME and $0 are real global variables, and you can change their values. $PROGRAM_NAME and $0 are aliases to each other, so you change the value of either one, the value of the other will change accordingly. For example, you have a test2.rb like this:

与__FILE__不同,$PROGRAM_NAME和$0是真正的全局变量,您可以更改它们的值。$PROGRAM_NAME和$0是彼此的别名,因此您可以更改其中一个的值,另一个的值将相应地更改。例如,您有一个test2。rb:

#!/usr/bin/env ruby
$0 = 'Hello, world!'
puts $0
puts $PROGRAM_NAME

it prints

它打印

Hello, world!
Hello, world!

#2


1  

__FILE__ is the current source file name.

__FILE__是当前的源文件名。

It seems to ruby code wants to make sure that the current file corresponds to the program that needs to executed.

ruby代码似乎希望确保当前文件对应于需要执行的程序。

$ before a variable, means its a global variable.

$在变量之前,意味着它是一个全局变量。

Check here to know more - http://www.zenspider.com/Languages/Ruby/QuickRef.html#18

点击这里了解更多信息——http://www.zenspider.com/Languages/Ruby/QuickRef.html#18