Issue #2545 has been updated by Dave B.


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.

Thanks to Nobu for working a patch.

daz


----------------------------------------
Bug #2545: Array#delete_if is borked if user calls 'break'
http://redmine.ruby-lang.org/issues/2545

Author: Brian Candler
Status: Assigned
Priority: Normal
Assignee: Yukihiro Matsumoto
Category: core
Target version: Ruby 1.8.7
ruby -v: ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux] 


=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]
 
 >> RUBY_VERSION
 => "1.8.7"
 >> RUBY_PATCHLEVEL
 => 174
=end



-- 
http://redmine.ruby-lang.org