Managing warnings emitted by Ruby 2.7

Ruby 2.7 is released on Christmas 2019.

If you don't know what Ruby 2.7 offers, I have you covered.

I tried running codetriage on Ruby 2.7. When I run rails c it prints tons of warnings about various deprecations.

/Users/prathamesh/.rbenv/versions/2.7.0-rc2/lib/ruby/gems/2.7.0/gems/mustermann-1.0.3/lib/mustermann/regular.rb:22: warning: The called method `initialize' is defined here
/Users/prathamesh/.rbenv/versions/2.7.0-rc2/lib/ruby/gems/2.7.0/gems/sinatra-2.0.7/lib/sinatra/base.rb:1604: warning: The last argument is used as keyword parameters; maybe ** should be added to the call
/Users/prathamesh/.rbenv/versions/2.7.0-rc2/lib/ruby/gems/2.7.0/gems/sinatra-2.0.7/lib/sinatra/base.rb:1621: warning: The called method `compile!' is defined here
/Users/prathamesh/.rbenv/versions/2.7.0-rc2/lib/ruby/gems/2.7.0/gems/mustermann-1.0.3/lib/mustermann/pattern.rb:59: warning: The last argument is used as keyword parameters; maybe ** should be added to the call
....
Most of the warnings are related to big keyword arguments related change.

Even running rails server prints all of these warnings before showing actual output. Fortunately, we can silence these deprecation warnings easily. Along with introducing lot of deprecation warnings, Ruby 2.7 has also provided a way out by enhancing the command line W flag.

When starting rails c we can silence the deprecation warnings as follows:

RUBYOPT=-W:no-deprecated rails c
➜ RUBYOPT=-W:no-deprecated rails c
/Users/prathamesh/.rbenv/versions/2.7.0-rc2/lib/ruby/gems/2.7.0/gems/json-2.2.0/lib/json/common.rb:156: warning: The last argument is used as keyword parameters
/Users/prathamesh/.rbenv/versions/2.7.0-rc2/lib/ruby/gems/2.7.0/gems/tzinfo-1.2.5/lib/tzinfo/ruby_core_support.rb:142: warning: The last argument is used as keyword parameters
/Users/prathamesh/.rbenv/versions/2.7.0-rc2/lib/ruby/gems/2.7.0/gems/tzinfo-1.2.5/lib/tzinfo/ruby_core_support.rb:142: warning: The last argument is used as keyword parameters
Loading development environment (Rails 6.0.2)
This version of IRB is drastically different from the previous version.
If you hit any issues, you can use "irb --legacy" to run the old version.
If you want to just erase this message, please use "irb --multiline" or
add `IRB.conf[:USE_MULTILINE] = true` to your ~/.irbrc file.

This prints out rest of the warnings which are not about deprecations.

Ruby 2.7 also comes with lot of experimental features such as pattern matching. If you use pattern matching, you get a warning.

irb(main):001:1* case params
irb(main):002:1*   in { name: a, email: b }
irb(main):003:1*     p a
irb(main):004:1*     p b
irb(main):005:0> end
(irb):1: warning: Pattern matching is experimental, and the behavior may change in future versions of Ruby!

If we don't want to see the warnings about experimental features, we can silence them by using W:no-experimental flag.

Finally, we can combine above two flags to get rid of deprecation warnings as well as warnings about experimental features.

RUBYOPT='-W:no-deprecated -W:no-experimental' rails c

Most of the gems are being updated to fix the deprecations from Ruby 2.7 but till then if you want a nice and clean prompt without any deprecation warnings from Ruby, try this trick!

Note that, silencing warnings is not a good way to handling them. But because Ruby 2.7 emits so many warnings, you can try this trick to run rails console or one-off scripts. But proper way to manage warnings is to fix them and updating the gems which fix them.


Want to know more about Ruby 2.7? Subscribe to my mailing list.

Show Comments
>