Rails 7 deprecates Enumerable#sum and Array#sum

Aashish Saini

Aashish Saini

June 22, 2021

This blog is part of our  Rails 7 series.

Rails 7 deprecates Enumerable#sum to the calls with non-numeric arguments. To ignore the warning we should use a suitable initial argument.

Before Rails 7

=> %w[foo bar].sum
=> "foobar"

=> [[1, 2], [3, 4, 5]].sum
=> [1, 2, 3, 4, 5]

After Rails 7

=> %w[foo bar].sum
=> Rails 7.0 has deprecated Enumerable.sum in favor of Ruby's native implementation available since 2.4.
   Sum of non-numeric elements requires an initial argument.

=> [[1, 2], [3, 4, 5]].sum
=> Rails 7.0 has deprecated Enumerable.sum in favor of Ruby's native implementation available since 2.4.
   Sum of non-numeric elements requires an initial argument.

To avoid the deprecation warning, we should use suitable argument as below.

=> %w[foo bar].sum('')
=> "foobar"

=> [[1, 2], [3, 4, 5]].sum([])
=> [1, 2, 3, 4, 5]

Check out this pull request for more details.

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.