Back to Blog

Rails 7 deprecates Enumerable#sum and Array#sum

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.


You might also like

If you liked this blog post, check out similar ones from BigBinary