Issue #12902 has been updated by Aaron Lasseigne.


Martin,

I would expect the empty to act like `reduce` and return `nil`.

Akira,

I agree that doing string concatenation with `sum` is a poor idea. It's just that this is on `Enumerable` which is supposed to allow iterations over whatever underlying data you want and this is favoring a particular type of underlying data. I do understand that `0` is convenient for the vast majority of use cases, it just feels like `Numeric` is creeping into `Enumerable`.

Thanks for considering it and responding.


----------------------------------------
Feature #12902: How about Enumerable#sum uses initial value rather than 0 as default?
https://bugs.ruby-lang.org/issues/12902#change-61448

* Author: Aaron Lasseigne
* Status: Feedback
* Priority: Normal
* Assignee: 
----------------------------------------
In https://bugs.ruby-lang.org/issues/12217#note-3, Akira Tanaka, mentions that the default argument to sum is 0. This creates problems with non-numeric summations (e.g. strings). This would make the method more flexible. It also makes it behave more like `reduce`. I think using the initial value in the enumerable is less surprising than using 0.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>