“#!/usr/local/bin/ruby -w"有什么用?在 ruby​​ 程序开始时 [英] what is the use of "#!/usr/local/bin/ruby -w" at the start of a ruby program

查看:89
本文介绍了“#!/usr/local/bin/ruby -w"有什么用?在 ruby​​ 程序开始时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ruby​​ 程序的开头写下面的命令有什么用?

what is the use of writing the following command at the start of a ruby program ?

#!/usr/local/bin/ruby -w

它是操作系统特定的命令吗?它对 windows 上的 ruby​​ 有效吗?如果不是,那么 Windows 中的等效命令是什么?

Is it OS specific command? Is it valid for ruby on windows ? if not, then what is an equivalent command in windows ?

推荐答案

Shebang 行是可选的,如果您运行 ruby​​ 解释器并将脚本作为命令行参数传递给它,那么您在命令上设置的标志线是 ruby​​ 运行的标志.

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​​ 注释).这真的是shell脚本.大多数 linux 和 unix 用户都在运行 BASH shell(代表 Borne Again SHell),但几乎每个操作系统都有一个命令解释器来支持 Shebang.

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.

#!/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.

在 Windows 上,Shell"是命令提示符,但即使没有黑色 DOS 窗口,命令解释器也会根据文件关联运行脚本.命令解释器或编程语言是否正在读取shebang并确保标志受到尊重并不重要,重要的是,他们受到尊重.

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.

-w"是一个标志.基本上,这是 ruby​​ 在运行脚本时要遵循的指令.在这种情况下,-w"会打开警告,因此在脚本执行期间您会收到额外的警告(脚本继续运行)或错误(脚本停止运行).可以在程序期间捕获警告和异常并对其采取行动.这些有助于程序员发现导致意外行为的问题.

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.我也是高质量可重用编码的粉丝,所以一定要使用 -w.为正确的工作提供正确的工具.如果您正在学习,请始终使用 -w.当您知道自己在做什么,并停止在快速任务上使用 -w 时,您将开始弄清楚何时使用 -w 会有所帮助,而不是花费数小时来解决问题.(提示,当问题的原因不是很明显时,只需添加 -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" 需要一些额外的编码来让 ruby​​ 清楚你的意思,所以它不会立即解决问题,但是如果你已经用 -w 编写了代码,那么你添加使小脚本运行并带有警告的必要位.事实上,如果您习惯使用 -w,那么您可能已经在以这种方式编写代码,并且 -w 不会改变任何内容,除非您忘记了某些内容.Ruby 需要的管道代码"远少于大多数(可能是所有)编译语言(如 C++),因此选择不使用 -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).

-v 是详细模式,不会改变脚本的运行(不会引发警告,不会在新位置停止脚本).一些站点和讨论调用 -w 详细模式,但 -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.

这篇关于“#!/usr/local/bin/ruby -w"有什么用?在 ruby​​ 程序开始时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆