From: Massimiliano Mirra Date: 2002-11-16T02:09:28+09:00 Subject: Re: Ruby equivalent to Python's map()? On Thu, Nov 14, 2002 at 10:39:44PM +0900, Roberto Amorim wrote: > The first parameter above can be any function taking 2 parameters and > returning a tuple. A ruby equivalent would be something of the form: > > map(a,b) {|i,j| i,j} Just a quick hack, but you can use this: class Array def zip(*arrays) zipped = [] all = [self, *arrays] max_length = all.collect {|a| a.length}.max 0.upto(max_length-1) do |i| zipped << all.collect {|a| a[i]} end return zipped end def each2(*arrays, &block) zip(*arrays).each(&block) end def collect2(*arrays, &block) zip(*arrays).collect(&block) end end a = [1, 2, 3] b = [4, 5, 6] c = [7, 8, 9] a.each2(b) {|i, j| ...} a.each2(b, c) {|i, j, k| ...} collect2 just behaves as you'd expect it to. Massimiliano