summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--array.c9
-rw-r--r--test/ruby/test_array.rb3
2 files changed, 9 insertions, 3 deletions
diff --git a/array.c b/array.c
index d9d6bdb..1820468 100644
--- a/array.c
+++ b/array.c
@@ -5734,7 +5734,7 @@ rb_ary_dig(int argc, VALUE *argv, VALUE self)
}
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 v)
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 ary)
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)) {
/*
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 0810014..b0be0b4 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -2838,6 +2838,9 @@ class TestArray < Test::Unit::TestCase
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