37

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 ?

3 Answers 3

76

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.

5
  • I think -w refers to enabling verbose mode or simply enabling error reporting. Since #!/usr/local/bin/ruby -w does not make any sense for windows, for file associations do we need to add any statement at the beginning of the program ?
    – simminni
    Jul 3, 2013 at 12:14
  • For Windows, just associate .rb files with the Ruby executable, wherever that is. Jul 3, 2013 at 12:14
  • It not entirely correct to say that the shebang line has no effect on Windows. See my answer for one example.
    – FMc
    Jul 3, 2013 at 12:57
  • If you had not said this, I would have been under an impression that shebang line is not required. Thanks for the example.
    – simminni
    Jul 3, 2013 at 13:38
  • This is good answer. But one thing this answer forgot to mention, which is also important mention in this answere here. Jun 18, 2014 at 18:57
35

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.

1
  • Thank you for the detailed explanation of the -w flag! Dec 17, 2016 at 20:10
12

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
2
  • 2
    I am not sure if it is relevant to ask this here, but can you please tell me what is the role of VERBOSE global variable ?
    – simminni
    Jul 3, 2013 at 13:40
  • 1
    @simminni If you run ruby -h you'll get a basic (but not very helpful) answer: the -w flag will "turn warnings on for your script". See this post for some more context: mislav.uniqpath.com/2011/06/ruby-verbose-mode. My advice: if your script depends on $VERBOSE having a specific value, set it explicitly in the script (don't rely on a shebang line flag).
    – FMc
    Jul 3, 2013 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.