Managing warnings emitted by Ruby 2.7
Tags: ruby
Published: 2019-12-30 04:45:42

---
title: Managing warnings emitted by Ruby 2.7
published: true
date: 2019-12-26 17:20:59 UTC
tags: ruby
canonical_url: https://prathamesh.tech/2019/12/26/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.](https://dev.to/prathamesh/all-you-need-to-know-about-ruby-2-7-1mp5)

I tried running [codetriage](https://github.com/codetriage/codetriage) on Ruby 2.7. When I run `rails c` it prints tons of warnings about various deprecations.

<!--kg-card-begin: code-->
```rb
/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
....
```
<!--kg-card-end: code-->

> Most of the warnings are related to big [keyword arguments related change.](https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/)

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:

<!--kg-card-begin: code-->
```rb
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.
```
<!--kg-card-end: code-->

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.

<!--kg-card-begin: code-->
```rb
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!
```
<!--kg-card-end: code-->

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


<!--kg-card-begin: hr-->
* * *
<!--kg-card-end: hr-->

Want to know more about Ruby 2.7? Subscribe to my [mailing list](https://prathamesh.tech/mailing-list/).