在 ruby 程序开始时使用“#!/usr/local/bin/ruby -w”有什么用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17447532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is the use of "#!/usr/local/bin/ruby -w" at the start of a ruby program
提问by simminni
what is the use of writing the following command at the start of a ruby program ?
在 ruby 程序开始时编写以下命令有什么用?
#!/usr/local/bin/ruby -w
Is it OS specific command? Is it valid for ruby on windows ? if not, then what is an equivalent command in windows ?
它是操作系统特定的命令吗?它对 windows 上的 ruby 有效吗?如果不是,那么 Windows 中的等效命令是什么?
采纳答案by Able Mac
The Shebang line is optional, and if you run the ruby interpreter and pass the script to it as a command line argument, then the flags you set on the command line are the flags ruby runs with.
Shebang 行是可选的,如果您运行 ruby 解释器并将脚本作为命令行参数传递给它,那么您在命令行上设置的标志就是 ruby 运行时使用的标志。
A Shebang line is not ruby at all (unless you want to call it a ruby comment). It's really shell scripting. Most linux and unix users are running the BASH shell (stands for Borne Again SHell), but pretty much every OS has a command interpreter that will honor the Shebang.
Shebang 行根本不是 ruby(除非您想将其称为 ruby 注释)。这真的是shell脚本。大多数 linux 和 unix 用户都在运行 BASH shell(代表 Borne Again SHell),但几乎每个操作系统都有一个命令解释器来支持 Shebang。
“#!/usr/local/bin/ruby -w”
“#!/usr/local/bin/ruby -w”
The "she" part is the octothorp (#), aka pound sign, number sign, hash mark, and now hash tag (I still call it tic-tac-toe just cuz).
“她”部分是八叉戟 (#),又名磅符号、数字符号、井号,现在是井号(我仍然称它为井字游戏,只是因为它)。
The "bang" part is the exclaimation mark (!), and it's like banging your fist on the table to exclaim the command.
“砰”的部分是感叹号(!),就像用拳头敲桌子来喊出命令一样。
On Windows, the "Shell" is the command prompt, but even without a black DOS window, the command interpreter will run the script based on file associations. It doesn't really matter if the command interpreter or the programming langue is reading the shebang and making sure the flags are honored, the important point is, they are honored.
在 Windows 上,“Shell”是命令提示符,但即使没有黑色 DOS 窗口,命令解释器也会根据文件关联运行脚本。命令解释器或编程语言是否正在阅读shebang并确保标志受到尊重并不重要,重要的是,他们受到尊重。
The "-w" is a flag. Basically it's an instruction for ruby to follow when it runs the script. In this case "-w" turns on warnings, so you'll get extra warnings (script keeps running) or errors (script stops running) during the execution of the script. Warnings and exceptions can be caught and acted upon during the program. These help programmers find problems that lead to unexpected behavior.
“-w”是一个标志。基本上,这是 ruby 在运行脚本时要遵循的指令。在这种情况下,“-w”会打开警告,因此在脚本执行期间您会收到额外的警告(脚本继续运行)或错误(脚本停止运行)。可以在程序期间捕获警告和异常并对其采取行动。这些帮助程序员发现导致意外行为的问题。
I'm a fan of quick and dirty scripts to get a job done, so no -w. I'm also a fan of high quality reusable coding, so definitely use -w. The right tool for the right job. If you're learning, then always use -w. When you know what you're doing, and stop using -w on quick tasks, you'll start to figure out when it would have helped to use -w instead of spending hours trouble shooting. (Hint, when the cause of a problem isn't pretty obvious, just add -w and run it to see what you get).
我喜欢使用快速而肮脏的脚本来完成工作,所以没有 -w。我也是高质量可重用编码的粉丝,所以一定要使用 -w。为正确的工作提供正确的工具。如果您正在学习,请始终使用 -w。当您知道自己在做什么,并停止在快速任务上使用 -w 时,您将开始弄清楚何时使用 -w 会有所帮助,而不是花费数小时来解决问题。(提示,当问题的原因不是很明显时,只需添加 -w 并运行它以查看结果)。
"-w" requires some extra coding to make it clear to ruby what you mean, so it doesn't immediately solve things, but if you already write code with -w, then you won't have much trouble adding the necessary bits to make a small script run with warnings. In fact, if you're used to using -w, you're probably already writing code that way and -w won't change anything unless you've forgotten something. Ruby requires far less "plumbing code" then most (maybe all) compiled languages like C++, so choosing to not use -w doesn't allow you to save much typing, it just lets you think less before you try running the script (IMHO).
“-w”需要一些额外的编码来让 ruby 清楚你的意思,所以它不会立即解决问题,但如果你已经用 -w 编写代码,那么你将不会有太多的麻烦添加必要的位运行一个带有警告的小脚本。事实上,如果您习惯使用 -w,那么您可能已经在以这种方式编写代码,并且 -w 不会改变任何内容,除非您忘记了某些内容。Ruby 需要的“管道代码”远少于大多数(可能是所有)编译语言(如 C++),因此选择不使用 -w 不会让您节省太多输入,它只会让您在尝试运行脚本之前少思考(恕我直言)。
-v is verbose mode, and does NOT change the running of the script (no warnings are raised, no stopping the script in new places). Several sites and discussions call -w verbose mode, but -w is warning mode and it changes the execution of the script.
-v 是详细模式,不会更改脚本的运行(不会引发警告,不会在新位置停止脚本)。一些站点和讨论调用 -w 详细模式,但 -w 是警告模式,它会更改脚本的执行。
回答by Koterpillar
It is called a Shebang. It tells the program loader what command to use to execute the file. So when you run ./myscript.rb, it actually translates to /usr/local/bin/ruby -w ./myscript.rb.
它被称为舍邦。它告诉程序加载器使用什么命令来执行文件。因此,当您运行时./myscript.rb,它实际上会转换为/usr/local/bin/ruby -w ./myscript.rb.
Windows uses file associations for the same purpose; the shebang line has no effect (edit: see FMc's answer) but causes no harm either.
Windows 出于相同目的使用文件关联;shebang 行没有效果(编辑:见 FMc 的回答)但也不会造成伤害。
A portable way (working, say, under Cygwin and RVM) would be:
一种可移植的方式(例如,在 Cygwin 和RVM下工作)将是:
#!/usr/bin/env ruby
This will use the envcommand to figure out where the Ruby interpreter is, and run it.
这将使用env命令找出 Ruby 解释器的位置,并运行它。
Edit: apparently, precisely Cygwin will misbehave with /usr/bin/env ruby -wand try to look up ruby -winstead of ruby. You might want to put the effect of -winto the script itself.
编辑:显然,正是 Cygwin 会行为不端/usr/bin/env ruby -w并尝试查找ruby -w而不是ruby. 您可能希望将 的效果-w放入脚本本身。
回答by FMc
Although the execution behavior of a shebang line does not translate directly to the Windows world, the flags included on that line (for example the -win your question) do affect the running Ruby script.
尽管 shebang 行的执行行为不会直接转换为 Windows 世界,但该行中包含的标志(例如-w在您的问题中)确实会影响正在运行的 Ruby 脚本。
Example 1 on a Windows machine:
Windows 机器上的示例 1:
#!/usr/local/bin/ruby -w
puts $VERBOSE # true
Example 2 on a Windows machine:
Windows 机器上的示例 2:
#!/usr/local/bin/ruby
puts $VERBOSE # false

