From: Martin DeMello Date: 2003-08-30T00:38:44+09:00 Subject: Re: multiply all array with array ibotty wrote: > thx all ya guys, > >>> is there a builtin function in array.rb (that i have missed). >> >> Not quite, but for parallel traversing made easier >> >> a.zip(b).map {|i, j| i*j} > > this is indeed much more elegant. > > but because array * array is not defined, me thinks. one could implement > (this simple case) this way. Here's a handy generalisation of the method: class Array def mapwith(*args, &block) if block_given? zip(*args).collect {|i, j| yield i, j} else f = args.pop zip(*args).collect {|i, j| i.send(f, j)} end end end p [1,2,3].mapwith([4,5,6]) {|i,j| i*j} p [1,2,3].mapwith([4,5,6], :*) martin