On 07.09.2007 20:15, Gordon Thiesfeld wrote:
> On 9/7/07, Chris McMahon <christopher.mcmahon / gmail.com> wrote:
>> There is probably a simple way to do this, but I'm drawing a blank:
>>
>> I have an arbitrarily large input array, like
>>
>> input = ['a','b','c','d','e','f','g']
>>
>> and three target arrays like
>>
>> targ_one = []
>> targ_two = []
>> targ_three = []
>>
>> I want to deal out the elements of this array into three target arrays
>> so they end up like
>>
>> ['a','d','g']
>> ['b','e']
>> ['c','f']
>>
> 
> This is the best I could come up with:
> 
> require 'enumerator'
> 
> input = ['a','b','c','d','e','f','g']
> input = *input.enum_slice(3)
> targ_one, targ_two, targ_three = input.shift.zip(*input).map{|i| i.compact }

Cute!  Here's my attempt:

$ irb -r enumerator
irb(main):001:0> a=%w{a b c d e f g h}
=> ["a", "b", "c", "d", "e", "f", "g", "h"]
irb(main):002:0> a.to_enum(:each_with_index).inject([[],[],[]]) 
{|ar,(x,i)| ar[i%ar.size] << x;ar}
=> [["a", "d", "g"], ["b", "e", "h"], ["c", "f"]]

Kind regards

	robert