Q:

How do I separate an array in Ruby?

Ok, so I have an array:
numbers = ["2", "3", "4", "5"]

and I need to split the array into two arrays with a conditional
numbers.reject!{|x| x > 4 }

and what i need is one array numbers to contain numbers = ["5"] and another array with the rejects rejects = ["2", "3", "4"]
How do I do this? ...It seems so easy with a loop but is there a way to do this in a one liner?

A:

Check out Enumerable#partition
arr = ["2", "3", "4", "5"]
numbers, rejects = arr.partition{ |x| x.to_i > 4 }
 # numbers = ["5"]
 # rejects = ["2", "3", "4"]

