Rails 6 has addedArray#extract!.
It returns the array elements for which the passed block returns true
and modifies the original array with remaining elements.
If no block is given, an Enumerator is returned instead.
Before
Lets say we want to separate out odd and even numbers
from an array.
We can achieve this by using select, reject or partition.
And if we use select!, reject!, delete_if or keep_if then:
Array#extract!
We can now use Array#extract! in Rails 6 to achieve this in a simpler and more readable way:
As you see, extract! returns elements which are odd,
and sets numbers with elements which are not odd.
This comes in pretty handy when we want to handle
both extracted elements and remaining elements differently.