Project

General

Profile

Actions

Bug #13222

closed

Array#sum inconsistency when init value is non-numeric

Added by matsuda (Akira Matsuda) over 5 years ago. Updated over 4 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.5.0dev (2017-02-17 trunk 57644) [x86_64-darwin15]
[ruby-core:79572]

Description

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"
Actions #1

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

  • Status changed from Open to Closed

Applied in changeset r57651.


array.c: check if numeric

  • array.c (finish_exact_sum): add 0 and the initial value to check
    if the latter is numeric. [ruby-core:79572] [Bug #13222]

Updated by mtsmfm (Fumiaki Matsushima) over 5 years ago

I confirmed this behavior appears not only 2.5.0dev but also 2.4.x.

$ docker run ruby:2.4.1 ruby -e "p [0].sum('a')"
"a"
$ docker run ruby:2.4.0 ruby -e "p [0].sum('a')"
"a"

Should we backport the patch?

Actions #3

Updated by usa (Usaku NAKAMURA) over 5 years ago

  • Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN to 2.2: DONTNEED, 2.3: DONTNEED, 2.4: REQUIRED

Updated by nagachika (Tomoyuki Chikanaga) over 4 years ago

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

Actions

Also available in: Atom PDF