How to Run All Ruby Scripts with Warnings

How can I run all Ruby scripts with warnings?

The RUBYOPT environment variable defines default options like warnings, etc.

Unix/OS X/etc:

export RUBYOPT=-w

You can put this in your startup script in Unix so it's set for new shells.

Windows:

set RUBYOPT=-w

Use the system properties dialog to set it for new shells/command windows.

How to run Ruby script with env and warnings enabled?

My first answer doesn't work on all systems, so here's another method: make your first non-comment line

$VERBOSE = true

which is What the -w switch does. From http://linux.die.net/man/1/ruby:

-v'

--verbose' Enables verbose mode. Ruby will print its version at the beginning, and set the variable $VERBOSE to true. Some methods print
extra messages if this variable is true. If this switch is given, and
no other switches are present, Ruby quits after printing its version.

-w' Enables verbose mode without printing version message at the beginning. It sets the $VERBOSE variable to true.

Unsure about correct permissions in running Ruby Scripts

Just run chmod og-w /Users/cs/Documents and all should be well.

This will remove the "other" and "group" write access permissions.

Ruby 2.7 specific warning not showing

I've discovered a few things. First of all, Ruby 2.7.2 and later don't show warnings by default which would explain the difference of behavior in my tests. To activate them you have to use RUBYOPT

# Ruby 2.7.5
$ RUBYOPT='-W:deprecated' ruby script.rb
script.rb:8: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
script.rb:1: warning: The called method `my_method' is defined here
hello

Another point is that the $VERBOSE = true you can set later on doesn't work either to show this warning. I'm not sure exactly why, but only RUBYOPT would do the job for this specific problem.

Finally, I also had used wrongly the RUBYOPT option when starting my Rails server locally thus not seeing it working.

I hope it helps.

How do I capture/control ERB warnings about already initialized constants?

Sergio's tip gave me the ability to do just what I wanted:

captured_err = StringIO.new
old_stderr = $stderr
$stderr = captured_err
# Can generate warnings to $stderr
result = erb.result
$stderr = old_stderr
captured_err.close
if (captured_err.size != 0) then
STDERR.puts "** #####"
STDERR.puts "** ERROR: Ruby code resulted in warnings"
captured_err.string.each_line { |line| STDOUT.puts " " + line }
exit 1
end

Suppress Ruby warnings when running specs

If you run your specs directly with the ruby command instead of the spec wrapper, you can use the -W command line option to silence warnings:

$ ruby --help
[...]
-W[level] set warning level; 0=silence, 1=medium, 2=verbose (default)

So in your case:

$ ruby -W0 -Ispec spec/models/event_spec.rb

should not show you any warnings.

Alternatively, you could set $VERBOSE=nil before your gems are loaded, ie at the top of your environment.rb (or application.rb if you're on Rails 3). Note that this disables all warnings all the time.

Or, since you are using Rails, you should be able to use Kernel.silence_warnings around the Bundler.require block if you're using Bundler:

Kernel.silence_warnings do
Bundler.require(:default, Rails.env) if defined?(Bundler)
end

More selectively, set $VERBOSE only for loading specific gems:

config.gem 'wellbehaving_gem'
original_verbosity = $VERBOSE
$VERBOSE = nil
config.gem 'noisy_gem_a'
$VERBOSE = original_verbosity

Erroneous Insecure world writable dir foo in PATH when running ruby script

You could shut off all warnings with

> ruby -W0 ...

But that may hide other issues. and you did say you want only that specific warning hidden, and I don't think there is a way to do it other than fix the issue, which I think is due to the NFS mount not properly relaying the actual mask. I see this when I mount a non-linux server on linux with NFS.

Like a snao server or something that does not support unix style attributes.

Also as the error is reporting that it doesn't like the world writable directory in the path, could you remove it from the path, and use a prefix to access anything in that directory?

EDIT...
Another idea is to filter the output of your ruby script with something like...

> ruby ... | egrep -v "warning: Insecure world writable dir"

That would print any output other (the -v) than the specific warning.

However the warning is a security warning, it is a bad idea to have a world writable directory in your path as anyone can put a malicious script or executable in there. And it is equally bad to have a mounted bin directory especially one you have no control over in your PATH. In this case the issue has nothing to do with whether the directory is writable or not, it is the fact there is a foreign directory in your PATH.

Good practices would dictate that you take that mounted directory out of your PATH and the warning will go away. If you need to execute something that is in that directory, then explicitly provide the full path to the script or executable.

This is not really a Ruby issue but a security issue.

How to run a Ruby script on a Github Workflow

As I've mentioned in the comments, the reason why your workflow isn't working is that you forgot the crucial step that checks-out your repository. By default, the workspace is empty unless you check out the repository with the Checkout GitHub Action.

From the GitHub Action's README:

This action checks-out your repository under $GITHUB_WORKSPACE, so your workflow can access it.

Here's an example:

- name: Checkout Repo
uses: actions/checkout@v2

(That's it, really)

(Note: You don't have to specify a name for the step.)



Related Topics



Leave a reply



Submit