* enum.c (enum_inject): Consider redefinition of Fixnum#+.

[ruby-dev:49510] [Bug#12178] Reported by usa.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54122 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2016-03-15 12:53:06 +00:00
Родитель da9b5e9c89
Коммит 32674b167b
3 изменённых файлов: 19 добавлений и 1 удалений

Просмотреть файл

@ -1,3 +1,8 @@
Tue Mar 15 21:38:28 2016 Tanaka Akira <akr@fsij.org>
* enum.c (enum_inject): Consider redefinition of Fixnum#+.
[ruby-dev:49510] [Bug#12178] Reported by usa.
Tue Mar 15 20:32:57 2016 Tanaka Akira <akr@fsij.org>
* enum.c (enum_inject): Implement the specialized code for :+ operator

3
enum.c
Просмотреть файл

@ -719,7 +719,8 @@ enum_inject(int argc, VALUE *argv, VALUE obj)
i = 0;
}
id = SYM2ID(op);
if (id == idPLUS && FIXNUM_P(v)) {
if (id == idPLUS && FIXNUM_P(v) &&
rb_method_basic_definition_p(rb_cFixnum, idPLUS)) {
long n = FIX2LONG(v);
while (i < RARRAY_LEN(obj)) {
VALUE e = RARRAY_AREF(obj, i);

Просмотреть файл

@ -184,6 +184,18 @@ class TestEnumerable < Test::Unit::TestCase
assert_equal(nil, @empty.inject() {9})
end
def test_inject_array_plus
assert_separately([], <<-"end;")
class Fixnum
undef :+
def +(x)
0
end
end
assert_equal(0, [1,2,3].inject(:+), "[ruby-dev:49510] [Bug#12178]")
end;
end
def test_partition
assert_equal([[1, 3, 1], [2, 2]], @obj.partition {|x| x % 2 == 1 })
cond = ->(x, i) { x % 2 == 1 }