You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, there is a certain logic to it b/c now it accommodates any form of summation, i.e. any thing that responds to #+. So for instance [[1],[2],[3]].sum works too (it produces [1,2,3]). The price for this flexibility however is that it will sometimes be necessary to give the identity, e.g. [].sum(0).
Thanks. This is a bit unfortunate b/c I really believe the more generalized form is better. Yes, it means passing an argument, e.g. sum(0), for the common case, but it makes you think about what you are doing, e.g. if summing floats. Ruby's version doesn't do that. Boo hiss! And b/c Ruby's is defined on Array instead of Enumerable it override Facets' method in the common cases. Not sure if Matz if right about this choice, it can go either way, it makes sense for some Enumerable classes and not others. In any case Facets will probably have to override Ruby's method to get the better behavior, but if so it has to be done in a compatible way, and that means parameterless sum has to return 0. ActiveSupport took an interesting approach and checked the final return value, if it is nil then it returns 0 instead. Maybe we can do that to, and with any luck there won't be any edge cases (i.e. Facets' sum will be a full superset of Ruby's). If not then the only choice is either convince Matz to change it in Ruby (hard to do) or rename the Facets method -- but to what?
Ruby does have a few weird things in it. I'd suggest that we file a bug report on Ruby.
To be honest, sum is a bit of a stupid method. It would even make more sense [].sum || "empty" but even this is not possible if as you say sum returns 0.
Facets 3.2.0 dropped its own Enumerable#sum in favor of Ruby's built-in (Ruby 2.4+), so [].sum returns 0 again. For non-numeric sums use sum(init) or reduce(:+).
Activity
trans commentedon Dec 5, 2015
Hmm... was my last generalization a bit too much? Have a look at the progression:
However, there is a certain logic to it b/c now it accommodates any form of summation, i.e. any thing that responds to #+. So for instance
[[1],[2],[3]].sumworks too (it produces[1,2,3]). The price for this flexibility however is that it will sometimes be necessary to give the identity, e.g.[].sum(0).What do you think?
boomerang commentedon Dec 7, 2015
I think is´t ok, but change the example documentation in the method :)
says that
[].sum => 0
I changed to active_support #sum it works as yours did before.
trans commentedon Dec 11, 2015
Ok, thanks. I updated the docs.
Honestly I am on the fence about whether Enumerable#sum should be generic or useful only for numerics.
ioquatix commentedon Oct 13, 2016
Ruby 2.4 now includes
#sumso be aware that this may cause issues. https://bugs.ruby-lang.org/issues/12217trans commentedon Oct 26, 2016
Thanks. This is a bit unfortunate b/c I really believe the more generalized form is better. Yes, it means passing an argument, e.g.
sum(0), for the common case, but it makes you think about what you are doing, e.g. if summing floats. Ruby's version doesn't do that. Boo hiss! And b/c Ruby's is defined on Array instead of Enumerable it override Facets' method in the common cases. Not sure if Matz if right about this choice, it can go either way, it makes sense for some Enumerable classes and not others. In any case Facets will probably have to override Ruby's method to get the better behavior, but if so it has to be done in a compatible way, and that means parameterlesssumhas to return0. ActiveSupport took an interesting approach and checked the final return value, if it isnilthen it returns0instead. Maybe we can do that to, and with any luck there won't be any edge cases (i.e. Facets' sum will be a full superset of Ruby's). If not then the only choice is either convince Matz to change it in Ruby (hard to do) or rename the Facets method -- but to what?ioquatix commentedon Dec 22, 2016
Ruby does have a few weird things in it. I'd suggest that we file a bug report on Ruby.
To be honest,
sumis a bit of a stupid method. It would even make more sense[].sum || "empty"but even this is not possible if as you say sum returns 0.ioquatix commentedon Dec 22, 2016
Ah, found this: https://bugs.ruby-lang.org/issues/12902 could be a good idea for us to chime in.
trans commentedon Nov 2, 2026
Facets 3.2.0 dropped its own
Enumerable#sumin favor of Ruby's built-in (Ruby 2.4+), so[].sumreturns0again. For non-numeric sums usesum(init)orreduce(:+).