Better way to split an array using Partition ⛓

Imagine you could, in one line, split an array into two arrays based on a condition 🤔

Now stop imagining, you can do exactly that with partition 🤩

Code snippet 📌

# ok: let's split an array to an odd array and even array
def split_odds_evens(nums)
  odds = []
  evens = []
  nums.each do |num|
    if num.odd?
      odds << num
    else
      evens << num
    end
  end
  [odds, evens]
end

# better: ruby helps you out with partition method!
def split_odds_evens(nums)
  nums.partition(&:odd?)
end

 split_odds_evens([1,2,3,4,5,6,7,8])
 [[1, 3, 5, 7], [2, 4, 6, ]]

Read more 🔖