" On Mon, 27 Nov 2000, Brian F. Feldman wrote:
" 
" > David Alan Black <dblack / candle.superlink.net> wrote:
" > > Hello --
" > > 
" > > Is there a nice Ruby idiom for interleafing arrays?  It's not hard to
" > > write a method that will do it, but I'd be interested in knowing how
" > > it's been done in the past.
" > 
" > What do you mean by interleave?  Normally I'd take this to mean that you'd 
" > want the even number of elements from the first array and the odd numbered 
" > elements to come from the second array, but it seems you're implying you 
" > just want the logical union of the two.  e.g.
" 
" Hmmmm.... I'm not sure what I said that implied that.  Maybe throwing
" in the hash thing was confusing.  Anyway, except for my misspelling
" the word (an old mental glitch), my reference was indeed to
" interleaving.
" 
" My current best shot at an interleave method is:
" 
" class Array
" 
"   def interleave(ary2)
"     a1, a2, res = self.dup, ary2.dup, []
"     res.push a1.shift, a2.shift until a1.empty? or a2.empty?
"     for n in [a1, a2]
"       res.concat n unless n.empty?
"     end
"     res
"   end
" 
" end
" 
" which is about as fast as I can get it (non-destructively :-)  

Or maybe something like this:

res = []
bigger = if a1.size > a2.size then a1 else a2
bigger.each_index { |n| res << [a1[n], a2[n]] }
res = res.flatten.compact

Of course, that only works if 'nil' is not a normal value in your
arrays.

--
Mike T. Miller
Senior Java Architect
Sun Microsystems, Switzerland
+41 1 812 04 51