Splitting an enumerable into two groups
Enumerable#partition is a method that returns two arrays.
The first array is everything the block evaluated to true, and the second array contains everything that was false.
numbers = [2, 95, 24, 27, 85, 4]
even, odd = numbers.partition { |num| num.even? }
=> [[2, 24, 4], [95, 27, 85]]
even
=> [2, 24, 4]
odd
=> [95, 27, 85]
Tweet