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

前端 未结 3 989
野的像风
野的像风 2020-12-24 01:47

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

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 02:12

    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.

提交回复
热议问题