ruby-warning
ruby-warning adds custom processing for warnings, including the ability to ignore specific warning messages, ignore warnings in specific files/directories, and add custom handling for all warnings in specific files/directories.
ruby-warning requires ruby 2.4+, as previous versions of ruby do not support custom processing of warnings.
Installation
gem install warning
Source Code
Source code is available on GitHub at github.com/jeremyevans/ruby-warning
Usage
By default, requiring the library does not make changes to how ruby processes warnings, it just adds methods that allow you to customize the processing.
Warning.ignore
takes a regexp and optionally a path prefix, and ignores any warning that matches the regular expression if it starts with the path prefix. It can also take a symbol or an array of symbols, and will use an appropriate regexp. The supported symbols are:
-
:arg_prefix
-
:ambiguous_slash
-
:bignum
-
:fixnum
-
:method_redefined
-
:missing_gvar
-
:missing_ivar
-
:not_reached
-
:shadow
-
:unused_var
-
:useless_operator
Warning.process
takes an optional path prefix and a block, and if the warning string starts with the path prefix, it calls the block with the warning string instead of performing the default behavior. You can call Warning.process
multiple times and it will operate intelligently, choosing the longest path prefix that the string starts with.
Warning.clear
just clears the current ignored warnings and warning processors.
By using path prefixes, it’s fairly easy for a gem to set that specific warnings should be ignored inside the gem’s directory.
Note that path prefixes will not correctly handle warnings raised by Kernel#warn
, unless the warning message given to Kernel#warn
starts with the filename where the warning is used.
Examples
# Ignore all uninitialized instance variable warnings
Warning.ignore(/instance variable @\w+ not initialized/)
# Ignore all uninitialized instance variable warnings in current file
Warning.ignore(/instance variable @\w+ not initialized/, __FILE__)
# Ignore all uninitialized instance variable warnings in current file
Warning.ignore(:missing_ivar, __FILE__)
# Ignore all Fixnum and Bignum warnings in current file
Warning.ignore([:fixnum, :bignum], __FILE__)
# Write warning to LOGGER at level warning
Warning.process do |warning|
LOGGER.warning(warning)
end
# Write warnings in the current file to LOGGER at level error
Warning.process(__FILE__) do |warning|
LOGGER.error(warning)
end
License
MIT
Author
Jeremy Evans <[email protected]>