From: Mark Hubbart Date: 2005-04-15T01:30:11+09:00 Subject: Re: Weaving Arrays On 4/13/05, Martin DeMello wrote: > galizur@gmail.com wrote: > > > > Any suggestions on a quick way to weave two arrays? > > This works as long as you pass it an array: > > class Array > def weave(other) > zip(other * (length / other.length + 1)).flatten > end > end If the result you want is: (1..4).weave('a'..'d') #==>[1, "a", 2, "b", 3, "c", 4, "d"] elegant: module Enumerable def weave(*others) zip(*others).inject([]){|ary,items| ary + items } end end efficient: module Enumerable def weave(*others) ary = [] zip(*others){|items| ary.push *items} ary end end cheers, Mark