Silencing Deprecation Warnings in Rails 3

Silencing Deprecation warnings in Rails 3

To silence all deprecation warnings you can do:

ActiveSupport::Deprecation.silenced = true

This could be placed in an initializer or in the environment file for a specific environment (e.g. to silence only in production for example.)

Or for a specific section of code, enclose it in a block:

ActiveSupport::Deprecation.silence do
# no warnings for any use of deprecated methods here
end

This works for both Rails 3 & 4.

How can I mute Rails 3 deprecation warnings selectively?

In fact I stil had lots of other deprecation warnings from code that was in plugins or gems I had installed. In order to avoid most of that, I overwrote the Deprecation::warn method in test_helper.rb. So instead of the previous code, use:

module ActiveSupport
module Deprecation
class << self
def warn(message = nil, callstack = caller)
# modif pvh the following lines make sure no deprecation warnings are sent
# for code that is
# not by my but in some gem or plugin...
return if silenced || callstack.grep(/myrailsappname/).blank?
# return if silenced
deprecation_message(callstack, message).tap do |m|
behavior.each { |b| b.call(m, callstack) }
end
end
end
end
end

BTW you need to replace myrailsappname with your app's name (the name of the folder it resides in). There is probably a more generic way to get that name, but I couldn't find it right now.

Silence deprecation warning generated from of a gem

ActiveSupport::Deprecation.silenced = true

Silencing Deprecation warnings in Rails 3

To silence all deprecation warnings you can do:

ActiveSupport::Deprecation.silenced = true

This could be placed in an initializer or in the environment file for a specific environment (e.g. to silence only in production for example.)

Or for a specific section of code, enclose it in a block:

ActiveSupport::Deprecation.silence do
# no warnings for any use of deprecated methods here
end

This works for both Rails 3 & 4.

Suppress Ruby warnings when running specs

If you run your specs directly with the ruby command instead of the spec wrapper, you can use the -W command line option to silence warnings:

$ ruby --help
[...]
-W[level] set warning level; 0=silence, 1=medium, 2=verbose (default)

So in your case:

$ ruby -W0 -Ispec spec/models/event_spec.rb

should not show you any warnings.

Alternatively, you could set $VERBOSE=nil before your gems are loaded, ie at the top of your environment.rb (or application.rb if you're on Rails 3). Note that this disables all warnings all the time.

Or, since you are using Rails, you should be able to use Kernel.silence_warnings around the Bundler.require block if you're using Bundler:

Kernel.silence_warnings do
Bundler.require(:default, Rails.env) if defined?(Bundler)
end

More selectively, set $VERBOSE only for loading specific gems:

config.gem 'wellbehaving_gem'
original_verbosity = $VERBOSE
$VERBOSE = nil
config.gem 'noisy_gem_a'
$VERBOSE = original_verbosity

How to suppress deprecation warnings?

The warning is generated by the parser before the code is evaluated. That includes your Warning.ignore code, so the config isn't ready when the warning occurs.

You can move that code into a separate file:

# warn.rb
require 'warning'
Warning.ignore(/deprecated/)

and load it via -r:

$ ruby -r./warn.rb -e '0 if /a/.../b/'

Is there a way to silence Ruby's deprecation warning in 2.4.0?


module Kernel
def suppress_warnings
original_verbosity = $VERBOSE
$VERBOSE = nil
result = yield
$VERBOSE = original_verbosity
return result
end
end


>> X = :foo
=> :foo
>> X = :bar
(irb):11: warning: already initialized constant X
=> :bar
>> suppress_warnings { X = :baz }
=> :baz


Related Topics



Leave a reply



Submit