Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Enumerable#partition

enum.partition { |obj| block } → [ true_array, false_array ]
enum.partition → an_enumerator

Returns two arrays, the first containing the elements of enum for which the block evaluates to true, the second containing the rest.

If no block is given, an enumerator is returned instead.

Example
(1..6).partition { |v| v.even? }  #=> [[2, 4, 6], [1, 3, 5]]
See also #one? #reduce #none? #reject also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1