Silence/ignore particular warnings from some parts of the code (in a .rb file)
I would like to make it possible to briefly enable or disable particular warnings: those related to modifying a constant or a method.
It is possible to silence a specific warning as in:
SOME_CONSTANT = 42 old_verbose_value = $VERBOSE $VERBOSE = nil SOME_CONSTANT = 24 $VERBOSE = old_verbose_value
I needed to do so to redefine a method. I had to add an instance variable to that method. There are alternatives, e.g. including a module, or subclassing, but I wanted to just redefine the method as is, without incurring a warning message on the command line. The above way to re-assign $VERBOSE works fine. It should be kept. At the same time, though, looking at $variables is not that elegant, and it feels a tiny bit hackish too; plus, it may be useful if ruby users may use a more common idiom for this procedure.
I found out that rails has this:
It may be elegant to have a method, be it in Kernel, or in Warnings, or another particular name (silence_warnings is not a bad name).
There may be more use cases, but I am only thinking about these two use cases.
I refer ONLY to situations where the ruby developer would be aware that a warning would be silenced. I run with -w all the time, but not every warning is equally useful to me.
It may be helpful if you could comment a use case for something like this. It may be also helpful if others comment whether the use case may be useful
We can only settle for a single short method. Otherwise, the current way would be better.
Updated by jeremyevans0 (Jeremy Evans) about 3 years ago
- Status changed from Open to Feedback
Warning.warn added in ruby 2.4 offers the core infrastructure that allows this. The warning gem (https://rubygems.org/gems/warning) offers a nice API to support the filtering of such warnings, assuming they are correctly prefixed by the filename in which the warning occurs.
rb_warning in the C-API both use that format, as does
Kernel#warn with the
:uplevel option. Do you see a need for an additional feature in this area, and if so, what do you think the API should look like?
Updated by sawa (Tsuyoshi Sawada) about 3 years ago
- Subject changed from Consider providing ruby developers with a way to specifically silence/ignore particular warnings from some parts of the code (in a .rb file) to Silence/ignore particular warnings from some parts of the code (in a .rb file)
- Description updated (diff)
Updated by Eregon (Benoit Daloze) about 3 years ago
It's also worth noting that changing $VERBOSE is not thread-safe (it's a truly global variable) and might suppress more or less warnings than intended.