Martin DeMello <martindemello / yahoo.com> wrote:
> Sabby and Tabby <sabbyxtabby / yahoo.com> wrote:
>> 
>> Should #mapwith work with 3 or more arrays like #zip does?
>> 
>>  p [1,2,3].mapwith([4,5,6], [7,8,9]) {|i,j,k| i*j+k}

class Array
  def mapwith(*args, &block)
    if block_given?
      zip(*args).collect {|i| yield *i}
    else
      f = args.pop
      zip(*args).collect {|i, *j| i.send(f, *j)}
    end
  end
end
 
p [1,2,3].mapwith([4,5,6], [7,8,9]) {|i,j,k| i*j+k}
p [[1],[2],[3]].mapwith([4,5,6], [7,8,9], :push)

martin