what is the use of #!/usr/local/bin/ruby -w at the start of a ruby program
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.
/usr/bin/ruby vs. rvm
the only sane way to go is:
#!/usr/bin/env ruby
it will always use current selected ruby in the environment, not depending on any tool
When trying to install pg gem: Brew bad interpreter error and not existing ruby version
Okay, I got it - I've edited the file: /usr/local/bin/brew
and edited ther (first line) the path: from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin
to /usr/local/bin/ruby/
and it works now..
Anyone knows how to change default system Ruby path to this one? (to avoid problems with other programs in the future).
Which ruby version am I using?
If ruby -v
shows 2.6.3, then you are using the ruby which ships with the system by default. You can confirm this in several ways. Running the shell command which ruby
will show /usr/bin/ruby
. Checking your PATH
will show /usr/bin
earlier than the location of the homebrew or rbenv installations.
If you wish to run one of the others, you can put it earlier on the PATH
or invoke it explicitly by giving the fully qualified name such as /usr/local/opt/ruby/bin/ruby myscript.rb
. Another alternative which avoids twiddling the PATH
variable is to use a shebang line at the beginning of different scripts pointing explicitly to the version to use with that script.
Ruby 1.9 If I have a shebang pointing to ruby, why doesn't the system see it?
You need to first make your script executable:
chmod +x todo.rb
then you need to run it like so:
./todo.rb
You cannot run it by just saying todo.rb
, unless you place it in your PATH
, in which case you can do so from anywhere.
Use non-system ruby to execute ruby scripts within macOS App
Due to Martin R's help I was able to solve the issue.
For now I deactivated macOS sandboxing in the projects capabilities.
Now I can use any ruby I want.
Thank you
bash: /usr/bin/ruby: No such file or directory
Per the comments on the question, your /usr/bin/ruby
binary is a symlink to /etc/alternatives/ruby
.
lrwxrwxrwx 1 root root 22 Aug 12 20:11 /usr/bin/ruby -> /etc/alternatives/ruby
You should confirm that path exists (run ls -la /etc/alternatives/ruby
to check if that path exists) and if it does not, you'll need to reinstall Ruby using your system package manager (e.g., apt-get
), download and install Ruby from https://www.ruby-lang.org, or use a tool like RVM.
Related Topics
How to Fix a Deadlock Caused by Open
Why Doesn't 'User Installation Directory' Match with the Ruby Version
Troubleshooting Ssl Certificates, Ruby, MAC Os X Yosemite
What Is the &: of &:Afunction Doing
How to Find Where Gem Files Are Installed
How to Step Out of a Loop with Ruby Pry
Rails: Convert Utc Datetime to Another Time Zone
What Is Mattr_Accessor in a Rails Module
How to Get the Intersection, Union, and Subset of Arrays in Ruby
File.Open, Open and Io.Foreach in Ruby, What Is the Difference
How to Resolve "Gpg: Command Not Found" Error During Rvm Installation
How to Update Ruby with Homebrew
Rails, How to Render a View/Partial in a Model