From: Devin Mullins Date: 2005-10-13T13:21:45+09:00 Subject: Re: array1 + array2 newb question AHH! Nevermind. I can't read. a1=[1,2,3] a2=%w{a b c} a2.map {|o2| a1.map {|o1| [o1,o2]}} Devin Devin Mullins wrote: >> I'm trying to create a map grid from arrayed names of streets. I >> want to go from [1,2,3] and [a,b,c] to [[1,a], [2,a], [3,a], >> [1,b],... [3,c]]. > > > > Given: > > a1=[1,2,3] > a2=%w{a b c} > > The brute force way: > > a = []; a1.length.times {|i| a << [a1[i],a2[i]]} > > The Ruby Way: > > a = a1.zip a2 > > Plenty of other ways, too, using inject, Enumerator, etc. I'm sure > they'll all be contributed in a few minutes. > > collect! is a whole 'nother beast -- it can be munged to do the task > at hand,* but isn't quite appropriate, since it doesn't tell the block > what index you're currently at. Have you read the ri documentation on it? > > Devin > > *i = -1; a1.collect! { i+=1; [a1[i],a2[i]] } > > >