Disable warnings on a per-file basis


Can I tell ruby to ignore warnings from a given .rb file?

$VERBOSE = nil

disables all warnings globally, so it does not work on a per file basis.

And I do not use commandline switches, I need this option in a given .rb

I googled and searched but found nothing. Ruby does not allow the
developer to ignore warnings from individual files? That is very bad.
The files I want to ignore are written by other people, not me. I have
no time to fix these warnings, I simply want ruby to ignore spamming me
with this useless information - but ONLY for those .rb files I define.

In ruby nothing is set in stone. I would try to find the method that
triggers the warnings (it is probably somewhere in Kernel), redefine it
and check for the code path/file parameter of the caller. If it
matches some custom pattern, mute the output, then delegate the action
to the original method via super and set the output as it was before.

The other place where you can do this is the require method that must
be called before some ruby file is loaded (or load method).

This might work for you:

CUSTOM_PATH = ‘/home/user/custom_code’

class Object
def require path
$VERBOSE = nil if File.expand_path("…/#{path}", FILE)
result = super
$VERBOSE = true

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs