module Warning::Processor
def clear
end
...
# Clear warning state inside the block
Warning.clear do
Warning.clear
# Clear warning state
Examples:
If a block is passed, the previous values are restored after the block exits.
Also disables deduplicating warnings if that is currently enabled.
Clear all current ignored warnings, warning processors, and duplicate check cache.
def clear if block_given? ignore = process = dedup = nil synchronize do ignore = @ignore.dup process = @process.dup dedup = @dedup.dup end begin clear yield ensure synchronize do @ignore = ignore @process = process @dedup = dedup end end else synchronize do @ignore.clear @process.clear @dedup = false end end end
def convert_regexp(regexp)
def convert_regexp(regexp) case regexp when Regexp regexp when Symbol IGNORE_MAP.fetch(regexp) when Array Regexp.union(regexp.map{|re| IGNORE_MAP.fetch(re)}) else # nothing end end
def dedup
has already occurred. Note that this can lead to unbounded memory use
Deduplicate warnings, suppress warning messages if the same warning message
def dedup @dedup = {} end
def freeze
def freeze @ignore.freeze @process.freeze super end
def ignore(regexp, path='')
# Ignore all uninitialized instance variable and method redefined warnings in current file
Warning.ignore(:missing_ivar, __FILE__)
# 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(/instance variable @\w+ not initialized/)
# Ignore all uninitialized instance variable warnings
Examples:
used (often used to trigger autoload).
:void_context :: Ignore warnings for :: to reference constants when the result is not
result is not used.
:useless_operator :: Ignore warnings when using operators such as == and > when the
:unused_var :: Ignore warnings for unused variables.
:taint :: Ignore warnings related to taint and related methods and C-API functions.
:shadow :: Ignore warnings related to shadowing outer local variables.
:safe :: Ignore warnings related to $SAFE and related C-API functions.
:not_reached :: Ignore statement not reached warnings.
that have not yet been initialized
:missing_ivar :: Ignore warnings for accesses to instance variables
that have not yet been initialized
:missing_gvar :: Ignore warnings for accesses to global variables
method of the same name was already defined in that class/module.
:method_redefined :: Ignore warnings when defining a method in a class/module where a
:keyword_separation :: Ignore warnings related to keyword argument separation.
:fixnum :: Ignore warnings when referencing the ::Fixnum constant.
in a future Ruby version.
:default_gem_removal :: Ignore warnings that a gem will be removed from the default gems
:bignum :: Ignore warnings when referencing the ::Bignum constant.
:ambiguous_slash :: Ignore warnings for things like method /regexp/
:arg_prefix :: Ignore warnings when using * or & as an argument prefix
use an appropriate regexp for the given warning:
The regexp can also be one of the following symbols (or an array including them), which will
start with the given path.
Ignore any warning messages matching the given regexp, if they
def ignore(regexp, path='') unless regexp = convert_regexp(regexp) raise TypeError, "first argument to Warning.ignore should be Regexp, Symbol, or Array of Symbols, got #{regexp.inspect}" end synchronize do @ignore << [path, regexp] end nil end
def process(path='', actions=nil, &block)
pass a symbol, which will be treated as a callable object that returns that symbol:
by Warning.ignore. Instead of passing a callable object as a value, you can
Instead of passing a regexp as a key, you can pass a symbol that is recognized
)
end
LOGGER.error(warning)
/global variable [`']\$\w+' not initialized/ => proc do |warning|
end,
LOGGER.warning(warning)
/instance variable @\w+ not initialized/ => proc do |warning|
Warning.process(__FILE__,
warnings, using regexp as keys and a callable objects as values:
Instead of passing a block, you can pass a hash of actions to take for specific
the warning and takes no other action.
If the block returns anything else, it is assumed the block completely handled
:raise :: Raise a RuntimeError with the warning as the message.
and also print the backtrace.
:backtrace :: Take the default action (call super, printing to $stderr),
:default :: Take the default action (call super, printing to $stderr).
The block can return one of the following symbols:
end
LOGGER.error(warning)
Warning.process(__FILE__) do |warning|
# Write warnings in the current file to LOGGER at level error level
end
LOGGER.warning(warning)
Warning.process do |warning|
# Write warning to LOGGER at level warning
the default behavior of printing them to $stderr. Examples:
Handle all warnings starting with the given path, instead of
def process(path='', actions=nil, &block) unless path.is_a?(String) raise ArgumentError, "path must be a String (given an instance of #{path.class})" end if block if actions raise ArgumentError, "cannot pass both actions and block to Warning.process" end elsif actions block = {} actions.each do |regexp, value| unless regexp = convert_regexp(regexp) raise TypeError, "action provided to Warning.process should be Regexp, Symbol, or Array of Symbols, got #{regexp.inspect}" end block[regexp] = ACTION_PROC_MAP[value] || value end else raise ArgumentError, "must pass either actions or block to Warning.process" end synchronize do @process << [path, block] @process.sort_by!(&:first) @process.reverse! end nil end
def synchronize(&block)
def synchronize(&block) @monitor.synchronize(&block) end