[ruby-dev:48811] [ruby-trunk - Bug #10722] Array#keep_if is borked if user calls 'break'
From:
akr@...
Date:
2015-01-10 13:47:13 UTC
List:
ruby-dev #48811
Issue #10722 has been updated by Akira Tanaka.
Apart from the performance problem, I feel following exmaple should show [7,8].
```
a = [5,6,7,8,9,10]; a.keep_if { |x| break if x > 8; x >= 7 }; p a
```
Because the method name is "keep_if", the method should keep only elements which the block returns true.
The block doesn't return true since "break" for 9 and 10.
So they should not be keeped.
This is similar (but reversed) to nagachika's comment for delete_if:
https://bugs.ruby-lang.org/issues/2545#note-6
----------------------------------------
Bug #10722: Array#keep_if is borked if user calls 'break'
https://bugs.ruby-lang.org/issues/10722#change-50919
* Author: _ wanabe
* Status: Closed
* Priority: Normal
* Assignee:
* Category:
* Target version:
* ruby -v: ruby 2.3.0dev (2015-01-09 trunk 49192) [x86_64-darwin14]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
ref. [Bug #2545]
```
$ ruby -e 'a = [5,6,7,8,9,10]; a.keep_if { |x| break if x > 8; x >= 7 }; p a'
[7, 8, 7, 8, 9, 10]
$ ruby -e 'a = [5,6,7,8,9,10]; a.delete_if { |x| break if x > 8; x < 7 }; p a'
[7, 8, 9, 10]
```
I was expecting the above scripts to be same results.
--
https://bugs.ruby-lang.org/