Module: Warning::Processor

Included in:
Warning
Defined in:
lib/warning.rb

Constant Summary collapse

IGNORE_MAP =

Map of symbols to regexps for warning messages to ignore.

{
  ambiguous_slash: /: warning: ambiguous first argument; put parentheses or a space even after `\/' operator\n\z/,
  arg_prefix: /: warning: `[&\*]' interpreted as argument prefix\n\z/,
  bignum: /: warning: constant ::Bignum is deprecated\n\z/,
  fixnum: /: warning: constant ::Fixnum is deprecated\n\z/,
  method_redefined: /: warning: method redefined; discarding old .+\n\z|: warning: previous definition of .+ was here\n\z/,
  missing_gvar: /: warning: global variable `\$.+' not initialized\n\z/,
  missing_ivar: /: warning: instance variable @.+ not initialized\n\z/,
  not_reached: /: warning: statement not reached\n\z/,
  shadow: /: warning: shadowing outer local variable - \w+\n\z/,
  unused_var: /: warning: assigned but unused variable - \w+\n\z/,
  useless_operator: /: warning: possibly useless use of [><!=]+ in void context\n\z/,
}

Instance Method Summary collapse

Instance Method Details

#clearObject

Clear all current ignored warnings and warning processors.



21
22
23
24
25
26
# File 'lib/warning.rb', line 21

def clear
  synchronize do
    @ignore.clear
    @process.clear
  end
end

#freezeObject



28
29
30
31
32
# File 'lib/warning.rb', line 28

def freeze
  @ignore.freeze
  @process.freeze
  super
end

#ignore(regexp, path = '') ⇒ Object

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.

: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.

:shadow

Ignore warnings related to shadowing outer local variables.

:unused_var

Ignore warnings for unused variables.

:useless_operator

Ignore warnings when using operators such as == and > when the result is not 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 uninitialized instance variable and method redefined warnings in current file
Warning.ignore([:missing_ivar, :method_redefined],  __FILE__)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/warning.rb', line 68

def ignore(regexp, path='')
  case regexp
  when Regexp
    # already regexp
  when Symbol
    regexp = IGNORE_MAP.fetch(regexp)
  when Array
    regexp = Regexp.union(regexp.map{|re| IGNORE_MAP.fetch(re)})
  else
    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

#process(path = '', &block) ⇒ Object

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


98
99
100
101
102
103
104
105
# File 'lib/warning.rb', line 98

def process(path='', &block)
  synchronize do
    @process << [path, block]
    @process.sort_by!(&:first)
    @process.reverse!
  end
  nil
end

#warn(str) ⇒ Object

Handle ignored warnings and warning processors. If the warning is not ignored and there is no warning processor setup for the warning string, then use the default behavior of writing to $stderr.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/warning.rb', line 110

def warn(str)
  synchronize{@ignore.dup}.each do |path, regexp|
    if str.start_with?(path) && str =~ regexp
      return
    end
  end

  synchronize{@process.dup}.each do |path, block|
    if str.start_with?(path)
      block.call(str)
      return
    end
  end

  super
end