Since Ruby 2.4.0, there has been a deprecation warning for using certain features that have been deprecated. For example, Bignum
, Fixnum
, TRUE
, and FALSE
will all trigger deprecation warnings. While I'm fixing my code, there is a fair amount of code that I would like it silenced for, especially in Rails. How can I do this?
Asked
Active
Viewed 1,611 times
6

Eli Sadoff
- 7,173
- 6
- 33
- 61
-
Possible duplicate of [Silencing Deprecation warnings in Rails 3](http://stackoverflow.com/questions/2689377/silencing-deprecation-warnings-in-rails-3) – Anthony Jan 15 '17 at 03:30
-
2@Anthony This is different. Deprecation Warnings are in Ruby 2.4.0 now, not just Rails. – Eli Sadoff Jan 15 '17 at 03:31
-
1Would running your code with `-W0` option work here? – Andrey Deineko Jan 15 '17 at 03:33
-
@AndreyDeineko For code that I write, but not for stuff that's baked deep in a gem. – Eli Sadoff Jan 15 '17 at 03:41
-
check [this](http://www.caliban.org/ruby/rubyguide.shtml#warnings) and [this](http://mislav.net/2011/06/ruby-verbose-mode/) out – Andrey Deineko Jan 15 '17 at 04:11
-
2Also, there's a [gem ruby-warning](https://github.com/jeremyevans/ruby-warning) (never used it though) – Andrey Deineko Jan 15 '17 at 04:40
1 Answers
3
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

user2012677
- 5,465
- 6
- 51
- 113
-
2This is good for code that I've written, but is there a way to get Ruby to run all code like this on the whole? – Eli Sadoff Jan 15 '17 at 16:24