Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress deprecation warnings?

(This is related to this question).

How can I suppress deprecation warnings in Ruby, until I find time to update the code? Example: The following command will generated a flip-flop is deprecated warning (the command itself is nonsense; I just wanted to provide a minimal, syntactically correct, example):

 ruby -v -e '0 if /a/.../b/'

I already tried

ruby -e '$VERBOSE=nil;0 if /a/.../b/'

and, after installing the warning gem,

 ruby -e 'require "warning";; Warning.ignore(/deprecated/) 0 if /a/.../b/'

but the warning is still printed to stderr. I don't want to redirect the whole stderr to the bit bucket, because I don't want to loose important messages.

like image 364
user1934428 Avatar asked Oct 27 '25 10:10

user1934428


1 Answers

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/'
like image 135
Stefan Avatar answered Oct 30 '25 04:10

Stefan