From: Dave Thomas Date: 2000-11-27T09:51:21+09:00 Subject: [ruby-talk:6585] Re: best way to interleaf arrays? David Alan Black writes: > 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. Matz has a technique for simulating external iterators using threads, which would do it. You could also do it the boring way: def interleave(a1, a2) n = a1.size n = a2.size if a2.size < n (0...n).collect {|i| [a1[i], a2[i]]}.flatten + a1[n..-1] + a2[n..-1] end On the Hash front, I'm starting to think it would be useful to have Hash.from_array(ary, value) which was equivalent to def Hash.from_array(ary, value = 1) h = {} if block_given? ary.each {|k| h[k] = yield(k,value) } else ary.each {|k| h[k] = value } end h end Regards Dave