summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--array.c23
-rw-r--r--test/ruby/test_array.rb10
3 files changed, 26 insertions, 12 deletions
diff --git a/ChangeLog b/ChangeLog
index c92a439182..8c90df7708 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sat Jan 10 10:12:15 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * array.c (rb_ary_select_bang): keep the array consistent by
+ removing unselected values soon. [ruby-dev:48805] [Bug #10722]
+
Fri Jan 9 23:20:04 2015 SHIBATA Hiroshi <shibata.hiroshi@gmail.com>
* lib/rubygems: Update to RubyGems HEAD(e53c54a).
diff --git a/array.c b/array.c
index 00d2a7f6a8..0de7231121 100644
--- a/array.c
+++ b/array.c
@@ -2843,23 +2843,22 @@ rb_ary_select(VALUE ary)
static VALUE
rb_ary_select_bang(VALUE ary)
{
- long i1, i2;
+ long i;
+ VALUE result = Qnil;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
rb_ary_modify(ary);
- for (i1 = i2 = 0; i1 < RARRAY_LEN(ary); i1++) {
- VALUE v = RARRAY_AREF(ary, i1);
- if (!RTEST(rb_yield(v))) continue;
- if (i1 != i2) {
- rb_ary_store(ary, i2, v);
+ for (i = 0; i < RARRAY_LEN(ary); ) {
+ VALUE v = RARRAY_AREF(ary, i);
+ if (!RTEST(rb_yield(v))) {
+ rb_ary_delete_at(ary, i);
+ result = ary;
+ }
+ else {
+ i++;
}
- i2++;
}
-
- if (i1 == i2) return Qnil;
- if (i2 < i1)
- ARY_SET_LEN(ary, i2);
- return ary;
+ return result;
}
/*
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 04e66cce11..31f33dde1e 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -2019,6 +2019,16 @@ class TestArray < Test::Unit::TestCase
a = @cls[ 1, 2, 3, 4, 5 ]
assert_equal(a, a.select! { |i| i > 3 })
assert_equal(@cls[4, 5], a)
+
+ bug10722 = '[ruby-dev:48805] [Bug #10722]'
+ a = @cls[ 5, 6, 7, 8, 9, 10 ]
+ r = a.select! {|i|
+ break i if i > 8
+ # assert_equal(a[0], i, "should be selected values only") if i == 7
+ i >= 7
+ }
+ assert_equal(9, r)
+ assert_equal(@cls[7, 8, 9, 10], a, bug10722)
end
def test_delete2