Phrogz
7/25/2007 11:02:00 PM
On Jul 25, 4:17 pm, Erik Boling <schmod...@yahoo.com> wrote:
> I was wondering, if it is possible to add two different arrays together,
> but do it line by line. For example: Array_1 has 1, 2 and 3, and
> Array_2 has 1, 2, and also 3, then your answer would come out 2, 4, and
> 6.
C:\>irb
irb(main):001:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> b = [4,5,6]
=> [4, 5, 6]
irb(main):003:0> a.zip(b)
=> [[1, 4], [2, 5], [3, 6]]
irb(main):004:0> a.zip(b).map{ |pair| pair[0] + pair[1] }
=> [5, 7, 9]
irb(main):006:0> module Enumerable; def sum; inject(0){ |s,v| s+v }
end; end
=> nil
irb(main):007:0> a.zip(b).map{ |pair| pair.sum }
=> [5, 7, 9]