=begin
Array is corrupted if you break out of a delete_if { ... } loop. I would expect that the elements already marked as deleted would be deleted, and the remainder of the array would be unchanged.
a = [5,6,7,8,9,10]
=> [5, 6, 7, 8, 9, 10]
a.delete_if { |x| break if x > 8; x < 7 }
=> nil
a
=> [7, 8, 7, 8, 9, 10]
At Sat, 2 Jan 2010 05:55:00 +0900,
Brian Candler wrote in [ruby-core:27366]:
Array is corrupted if you break out of a delete_if { ... }
loop. I would expect that the elements already marked as
deleted would be deleted, and the remainder of the array
would be unchanged.
The behavior would be an implementation detail, and should be
undefined (or implementation defined), I guess.
Index: array.c
===================================================================
--- array.c (revision 26229)
+++ array.c (working copy)
@@ -2307,7 +2307,18 @@ rb_ary_reject_bang(VALUE ary)
for (i1 = i2 = 0; i1 < RARRAY_LEN(ary); i1++) {
VALUE v = RARRAY_PTR(ary)[i1];
- if (RTEST(rb_yield(v))) continue;
if (i1 != i2) {
+ int state = 0;
+ if (RTEST(rb_protect(rb_yield, v, &state))) continue;
rb_ary_store(ary, i2, v);
+ if (state) {
+ VALUE *ptr = RARRAY_PTR(ary);
+ long len = RARRAY_LEN(ary);
+ MEMCPY(ptr + i2 + 1, ptr + i1 + 1, VALUE, len - i1 - 1);
+ ARY_SET_LEN(ary, len - i1 + i2);
+ rb_jump_tag(state);
+ }
+ }
+ else {
+ if (RTEST(rb_yield(v))) continue;
}
i2++;
Just met this problem:
ruby 1.8.7 (2011-02-18 patchlevel 334) [i386-mingw32]
I'd also consider it a bug and that the ruby implementation should be hidden from the user. Once an element has been selected for deletion, at the end of this iteration, it should be expected to be gone. When using very large arrays, where the programmer knows of a shortcut (e.g. the rest of the array need not be considered), s/he should be encouraged to handle it with 'break'. In testing, I was left wondering whether 'delete_if' was non-destructive, because nothing had changed, and started looking for a bang! method.
To achieve the current behaviour, I only need an Array#dup above the loop.
This issue was solved with changeset r32360.
Brian, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
array.c (rb_ary_reject_bang, rb_ary_delete_if): rejected
elements should be removed. fixed [Bug #2545]