Today I Learned

hashrocket A Hashrocket project

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]
See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.