what is the use of "#!/usr/local/bin/ruby -w" at the start of a ruby program

StackOverflow https://stackoverflow.com/questions/17447532

  •  02-06-2022
  •  | 
  •  

Question

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

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

Était-ce utile?

La solution 2

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.

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”

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.

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.

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" 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 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.

Autres conseils

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.

Windows uses file associations for the same purpose; the shebang line has no effect (edit: see FMc's answer) but causes no harm either.

A portable way (working, say, under Cygwin and RVM) would be:

#!/usr/bin/env ruby

This will use the env command to figure out where the Ruby interpreter is, and run it.

Edit: apparently, precisely Cygwin will misbehave with /usr/bin/env ruby -w and try to look up ruby -w instead of ruby. You might want to put the effect of -w into the script itself.

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 -w in your question) do affect the running Ruby script.

Example 1 on a Windows machine:

#!/usr/local/bin/ruby -w
puts $VERBOSE   # true

Example 2 on a Windows machine:

#!/usr/local/bin/ruby
puts $VERBOSE   # false
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top