Skip to content

Enumerable#sum for empty array #247

Closed

Description

@boomerang

I use Enumerable#sum a lot and just upgraded from version 2.9.3 to 3.0 and the behaviour changed.

[].sum should == 0 but i get nil instead.

Activity

  1. trans commented on Dec 5, 2015

    @trans
    Member

    Hmm... was my last generalization a bit too much? Have a look at the progression:

    1. ca4aae4
    2. 206c1f8
    3. 302e000

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

    What do you think?

  2. boomerang commented on Dec 7, 2015

    @boomerang
    Author

    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.

  3. trans commented on Dec 11, 2015

    @trans
    Member

    Ok, thanks. I updated the docs.

    Honestly I am on the fence about whether Enumerable#sum should be generic or useful only for numerics.

  4. ioquatix commented on Oct 13, 2016

    @ioquatix
    Contributor

    Ruby 2.4 now includes #sum so be aware that this may cause issues. https://bugs.ruby-lang.org/issues/12217

  5. trans commented on Oct 26, 2016

    @trans
    Member

    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?

  6. ioquatix commented on Dec 22, 2016

    @ioquatix
    Contributor

    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.

  7. ioquatix commented on Dec 22, 2016

    @ioquatix
    Contributor

    Ah, found this: https://bugs.ruby-lang.org/issues/12902 could be a good idea for us to chime in.

  8. trans commented on Nov 2, 2026

    @trans
    Member

    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(:+).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions