This could be done much more neatly with http://ruby-doc.org/core-2.0/Enumerable.html#method-i-partition