From: Gregory Brown Date: 2009-07-14T22:27:59+09:00 Subject: Elegant solution for inverting zip? Hi folks, I came across something I thought should have an obvious answer and came up blank. I'm hoping folks could give me a nice solution to this problem. Given the output below: >> a = [1,2,3,4].zip([5,6,7,8]) => [[1, 5], [2, 6], [3, 7], [4, 8]] I'd like to recover the original two arrays, something like unzip(a) #=> [[1,2,3,4],[5,6,7,8]] The best ordinary solution I could come up with was this: b = a.each_with_object([[],[]]) do |pair, memo| memo[0] << pair[0] memo[1] << pair[1] end Shorter, but perhaps weirder, is: require "matrix" b = Matrix[*a].column_vectors.map(&:to_a) Although either of these solutions are likely good enough for my needs, neither feel natural to me. Anyone have something better? -greg