Rails 7 deprecates Enumerable#sum and Array#sum

Aashish Saini

By Aashish Saini

on 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

1=> %w[foo bar].sum
2=> "foobar"
3
4=> [[1, 2], [3, 4, 5]].sum
5=> [1, 2, 3, 4, 5]

After Rails 7

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

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

1=> %w[foo bar].sum('')
2=> "foobar"
3
4=> [[1, 2], [3, 4, 5]].sum([])
5=> [1, 2, 3, 4, 5]

Check out this pull request for more details.

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, ReactJS, React Native, remote work,open source, engineering & design.