ruby-changes:45578
From: nobu <ko1@a...>
Date: Sat, 18 Feb 2017 13:23:26 +0900 (JST)
Subject: [ruby-changes:45578] nobu:r57651 (trunk): array.c: check if numeric
nobu 2017-02-18 13:23:20 +0900 (Sat, 18 Feb 2017) New Revision: 57651 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57651 Log: 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] Modified files: trunk/array.c trunk/test/ruby/test_array.rb Index: test/ruby/test_array.rb =================================================================== --- test/ruby/test_array.rb (revision 57650) +++ test/ruby/test_array.rb (revision 57651) @@ -2838,6 +2838,9 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2838 assert_equal("abc", ["a", "b", "c"].sum("")) assert_equal([1, [2], 3], [[1], [[2]], [3]].sum([])) + assert_raise(TypeError) {[0].sum("")} + assert_raise(TypeError) {[1].sum("")} + assert_separately(%w[-rmathn], <<-EOS, ignore_stderr: true) assert_equal(6, [1r, 2, 3r].sum) EOS Index: array.c =================================================================== --- array.c (revision 57650) +++ array.c (revision 57651) @@ -5734,7 +5734,7 @@ rb_ary_dig(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L5734 } static inline VALUE -finish_exact_sum(long n, VALUE r, VALUE v) +finish_exact_sum(long n, VALUE r, VALUE v, int z) { if (n != 0) v = rb_fix_plus(LONG2FIX(n), v); @@ -5747,6 +5747,9 @@ finish_exact_sum(long n, VALUE r, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L5747 else v = rb_rational_plus(r, v); } + else if (!n && z) { + v = rb_fix_plus(LONG2FIX(0), v); + } return v; } @@ -5831,11 +5834,11 @@ rb_ary_sum(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L5834 else goto not_exact; } - v = finish_exact_sum(n, r, v); + v = finish_exact_sum(n, r, v, argc!=0); return v; not_exact: - v = finish_exact_sum(n, r, v); + v = finish_exact_sum(n, r, v, i!=0); if (RB_FLOAT_TYPE_P(e)) { /* -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/