Mike Schramm wrote: > Hey all, > > I'm a ruby newbie, so this will be obvious, but I can't figure it out. > > 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]]. > > so far the closest I have gotten is > > a1.collect!{|x| [x, a2.collect!{|y| y}]} > > which returns-- well you can try it if you like, but it's not right. > Instead of just returning one value of a2 at a time, it returns the > whole thing every time. I'm guessing I need to somehow cycle through > each element of a2 (ie a2[0], a2[1], a2[2]), but I can't figure it > out. > > You guys will probably eat this up. What am I so obviously doing > wrong? > > Mike Since there hasn't been an #inject solution so far... :-) >> a1=[1,2,3] => [1, 2, 3] >> a2=%w{a b c} => ["a", "b", "c"] >> a1.inject([]) {|r,x| a2.each {|y| r << [x,y]}; r} => [[1, "a"], [1, "b"], [1, "c"], [2, "a"], [2, "b"], [2, "c"], [3, "a"], [3, "b"], [3, "c"]] And the double inject >> a1.inject([]) {|r,x| a2.inject(r) {|rr,y| rr << [x,y]}} => [[1, "a"], [1, "b"], [1, "c"], [2, "a"], [2, "b"], [2, "c"], [3, "a"], [3, "b"], [3, "c"]] Note the slight difference to the map approach >> a1.map {|x| a2.map {|y| [x,y]}} => [[[1, "a"], [1, "b"], [1, "c"]], [[2, "a"], [2, "b"], [2, "c"]], [[3, "a"], [3, "b"], [3, "c"]]] Here we have one nesting too much (compared to desired results given). Kind regards robert