Issue #12026 has been updated by Jeremy Evans.


Since this will be discussed tomorrow at the developers meeting, here's a more detailed example of how this can be used.

Let's say you are using a library that is not free of verbose warnings, but you would like to use verbose warnings in your application.  This is currently a pain as enabling warnings in your application will also print warnings for all libraries.  

With this feature, you can easily filter specific types of warnings for specific libraries, while still getting the benefits of verbose warnings for your application code. Example:

~~~
$WARNING_FILTER = %r{/gem_name(-[\d\.]+)?/lib/.+.rb:\d+: warning: instance variable \@\w+ not initialized\Z}
~~~

Having the filter be a global variable as opposed to an environment variable or CLI flag allows for additional flexibility, such as modifying the filter as runtime, which could be used for a block based filtering approach:

~~~
def filter_warnings(filter)
  prev = $WARNING_FILTER
  $WARNING_FILTER = filter
  yield
ensure
  $WARNING_FILTER = prev
end
~~~

Such an approach is not currently threadsafe, though I think $WARNING_FILTER could be made per-thread.

The attached patch is the minimally invasive way I could think of to support warning filters.  While using regexps for filtering warnings has some issues in regards to warning text changing, the text of ruby's warnings does not change frequently and it is possible to handle multiple filters and variations easily using Regexp.union.  So I don't expect the use of regexps for filtering warnings would be a problem in practice.

----------------------------------------
Feature #12026: Support warning filters
https://bugs.ruby-lang.org/issues/12026#change-57421

* Author: Jeremy Evans
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
This adds a simple way to filter warnings from being displayed. You
just set a $WARNING_FILTER with a regexp, and any warnings that
match the regexp will not be displayed.

I think this is a much simpler approach to filtering warnings than
feature #11588, while allowing the same type of capability.

This is backwards compatible, in that you can set $WARNING_FILTER in
previous versions of ruby without problems.

This should not cause any performance issues, as the regexp matching
isn't done until the warning message is about to be displayed.

It's possible to use something other than a global variable to store
the warning filter, but a global variable was the easiest way to
implement it, it has a global effect, and similar flags such as
$VERBOSE are also global variables, so I think a global variable
makes sense.

---Files--------------------------------
0001-Support-warning-filters.patch (4.12 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>