module Warning::Processor

Constants

ACTION_PROC_MAP

Map of action symbols to procs that return the symbol

IGNORE_MAP

Map of symbols to regexps for warning messages to ignore.

Public Instance Methods

clear() click to toggle source

Clear all current ignored warnings, warning processors, and duplicate check cache. Also disables deduplicating warnings if that is currently enabled.

   # File lib/warning.rb
35 def clear
36   synchronize do
37     @ignore.clear
38     @process.clear
39     @dedup = false
40   end
41 end
dedup() click to toggle source

Deduplicate warnings, supress warning messages if the same warning message has already occurred. Note that this can lead to unbounded memory use if unique warnings are generated.

   # File lib/warning.rb
46 def dedup
47   @dedup = {}
48 end
freeze() click to toggle source
Calls superclass method
   # File lib/warning.rb
50 def freeze
51   @ignore.freeze
52   @process.freeze
53   super
54 end
ignore(regexp, path='') click to toggle source

Ignore any warning messages matching the given regexp, if they start with the given path. The regexp can also be one of the following symbols (or an array including them), which will use an appropriate regexp for the given warning:

:arg_prefix

Ignore warnings when using * or & as an argument prefix

:ambiguous_slash

Ignore warnings for things like method /regexp/

:bignum

Ignore warnings when referencing the ::Bignum constant.

:fixnum

Ignore warnings when referencing the ::Fixnum constant.

:keyword_separation

Ignore warnings related to keyword argument separation.

:method_redefined

Ignore warnings when defining a method in a class/module where a method of the same name was already defined in that class/module.

:missing_gvar

Ignore warnings for accesses to global variables that have not yet been initialized

:missing_ivar

Ignore warnings for accesses to instance variables that have not yet been initialized

:not_reached

Ignore statement not reached warnings.

:safe

Ignore warnings related to $SAFE and related C-API functions.

:shadow

Ignore warnings related to shadowing outer local variables.

:taint

Ignore warnings related to taint and related methods and C-API functions.

:unused_var

Ignore warnings for unused variables.

:useless_operator

Ignore warnings when using operators such as == and > when the result is not used.

:void_context
Ignore warnings for

to reference constants when the result is not

used (often used to trigger autoload).

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 uninitialized instance variable and method redefined warnings in current file
Warning.ignore([:missing_ivar, :method_redefined],  __FILE__)
    # File lib/warning.rb
 95 def ignore(regexp, path='')
 96   unless regexp = convert_regexp(regexp)
 97     raise TypeError, "first argument to Warning.ignore should be Regexp, Symbol, or Array of Symbols, got #{regexp.inspect}"
 98   end
 99 
100   synchronize do 
101     @ignore << [path, regexp]
102   end
103   nil
104 end
process(path='', actions=nil, &block) click to toggle source

Handle all warnings starting with the given path, instead of the default behavior of printing them to $stderr. Examples:

# 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 level
Warning.process(__FILE__) do |warning|
  LOGGER.error(warning)
end

The block can return one of the following symbols:

:default

Take the default action (call super, printing to $stderr).

:backtrace

Take the default action (call super, printing to $stderr), and also print the backtrace.

:raise

Raise a RuntimeError with the warning as the message.

If the block returns anything else, it is assumed the block completely handled the warning and takes no other action.

Instead of passing a block, you can pass a hash of actions to take for specific warnings, using regexp as keys and a callable objects as values:

Warning.process(__FILE__,
  /instance variable @\w+ not initialized/ => proc do |warning|
    LOGGER.warning(warning)
  end,
  /global variable `\$\w+' not initialized/ => proc do |warning|
    LOGGER.error(warning)
  end
)

Instead of passing a regexp as a key, you can pass a symbol that is recognized by Warning.ignore. Instead of passing a callable object as a value, you can pass a symbol, which will be treated as a callable object that returns that symbol:

Warning.process(__FILE__, :missing_ivar=>:backtrace, :keyword_separation=>:raise)
    # File lib/warning.rb
146 def process(path='', actions=nil, &block)
147   if block
148     if actions
149       raise ArgumentError, "cannot pass both actions and block to Warning.process"
150     end
151   elsif actions
152     block = {}
153     actions.each do |regexp, value|
154       unless regexp = convert_regexp(regexp)
155         raise TypeError, "action provided to Warning.process should be Regexp, Symbol, or Array of Symbols, got #{regexp.inspect}"
156       end
157 
158       block[regexp] = ACTION_PROC_MAP[value] || value
159     end
160   else
161     raise ArgumentError, "must pass either actions or block to Warning.process"
162   end
163 
164   synchronize do
165     @process << [path, block]
166     @process.sort_by!(&:first)
167     @process.reverse!
168   end
169   nil
170 end

Private Instance Methods

convert_regexp(regexp) click to toggle source

Convert the given Regexp, Symbol, or Array of Symbols into a Regexp.

    # File lib/warning.rb
233 def convert_regexp(regexp)
234   case regexp
235   when Regexp
236     regexp
237   when Symbol
238     IGNORE_MAP.fetch(regexp)
239   when Array
240     Regexp.union(regexp.map{|re| IGNORE_MAP.fetch(re)})
241   else
242     # nothing
243   end
244 end
synchronize(&block) click to toggle source
    # File lib/warning.rb
246 def synchronize(&block)
247   @monitor.synchronize(&block)
248 end