Issue #13585 has been updated by phluid61 (Matthew Kerwin).


It's pretty clearly documented:

"If changes were made, it will return `self`, otherwise it returns `nil`."

----------------------------------------
Feature #13585: select! and reject! do not returns the same array in specific situations
https://bugs.ruby-lang.org/issues/13585#change-64966

* Author: sobrinho (Gabriel Sobrinho)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
When all elements are returned on select!, the method returns nil.

~~~
(main) normal> [1,2,3].select! { |x| x == 0 }
=> []
(main) normal> [1,2,3].select! { |x| x == 1 }
=> [1]
(main) normal> [1,2,3].select! { |x| x == 1 || x == 2 }
=> [1, 2]
(main) normal> [1,2,3].select! { |x| x == 1 || x == 2 || x == 3 }
=> nil
```

When all elements are kept on reject!, nil is returned.

~~~
(main) normal> [1,2,3].reject! { |x| x == 0 }
=> nil
(main) normal> [1,2,3].reject! { |x| x == 1 }
=> [2, 3]
(main) normal> [1,2,3].reject! { |x| x == 1 || x == 2 }
=> [3]
(main) normal> [1,2,3].reject! { |x| x == 1 || x == 2 || x == 3 }
=> []
~~~

This is surprising since it breaks code in very specific situations that most people won't notice easily:

~~~
blocked_assets = relation.to_a.select! { |asset| asset.blocked? }
unblocked_assets = relation.to_a.reject! { |asset| asset.unblocked? }
~~~



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>