From: Phillip Hutchings Date: 2006-07-05T09:21:24+09:00 Subject: Re: Parallel Array On 7/5/06, Nasir Khan wrote: > If I have two arrays a=[1,1,1,1] and b=[2,2,2,2] and I want to add each > element up, something like - > > c = a plus b => [3,3,3,3] > > One way I could think up is - > > irb(main):010:0> (0...a.length).collect{|k| a[k]+b[k]} > => [3, 3, 3, 3] > > Is there a better/faster way? > > Thanks > Nasir > > a.zip(b).collect{|x| x.inject{|q, r| q+r}} -- Phillip Hutchings http://www.sitharus.com/