From: Robert Klemme Date: 2005-08-29T18:42:02+09:00 Subject: Re: how to unflatten a flat-array 2005/8/29, SHIGETOMI, Takuhiko : > greetings, William. i'm sorry, your way is beyond my light-weight brain. Although I'm not the author I can jump in with an explanation. > > ary = [1,2,3,4,5,6,7,8] > > f=nil > > t=ary.partition{f=!f} > > p t[0].zip(t[1]) > > but it works... #partition puts contents of an Enumerable into two arrays depending on the boolean result of a block. For example: >> (0...10).partition {|x| x % 2 == 0} => [[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]] The code above just toggles a flag (f) on each evaluation of the block thus putting elements into the first and second array returned interchangingly. Array#zip basically iterates two array in lock step. So the first partition contains x elements and the second partition contains y elements and #zip creates pairs [x,y] from them. Although I like this solution because of its cuteness and shortness, it has the disadvantage of creating two intermediate arrays. That might or might not be a problem in your case - depending on the array sizes involved. HTH Kind regards robert