[ruby-core:84364] [Ruby trunk Bug#13222] Array#sum inconsistency when init value is non-numeric

From: nagachika00@...
Date: 2017-12-20 14:54:03 UTC
List: ruby-core #84364
Issue #13222 has been updated by nagachika (Tomoyuki Chikanaga).

Backport changed from 2.2: DONTNEED, 2.3: DONTNEED, 2.4: REQUIRED to 2.2: DONTNEED, 2.3: DONTNEED, 2.4: DONE

ruby_2_4 r61361 merged revision(s) 57649,57651.

----------------------------------------
Bug #13222: Array#sum inconsistency when init value is non-numeric
https://bugs.ruby-lang.org/issues/13222#change-68553

* Author: matsuda (Akira Matsuda)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.0dev (2017-02-17 trunk 57644) [x86_64-darwin15]
* Backport: 2.2: DONTNEED, 2.3: DONTNEED, 2.4: DONE
----------------------------------------
Since Array#sum skips calling plus for `0` https://github.com/ruby/ruby/blob/c85a58d/array.c#L5831-L5832, it doesn't raise when the receiver array consists of 0 only, even when the "init" value is non-numeric.

```
% ruby -e "p [1].sum('a')"
-e:1:in `sum': String can't be coerced into Integer (TypeError)
```

```
% ruby -e "p [0].sum('a')"
"a"
```

```
% ruby -e "p [0, 0, 0].sum('a')"
"a"
```




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

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

In This Thread

Prev Next