From: William James Date: 2006-09-14T06:20:41+09:00 Subject: Re: How to collect over two arrays then look back to one? Peter Booth wrote: > I am reading Ruby for Rails and Ruby Cookbock and delighted by the > Array#find_all and Enumerable#collect methods. I'm wondering what the > idiomatic way to do the following is? > > Two parallel arrays A, B . For each element n of A for which f(An) is true, > is g(B[n-3:n+3]) true? Where B[n-3:n+3] refers to seven elements of B whose > position is centered over An' position. > > Can the block inside Enumerable#collect know it's position inside the > Enumerable? Can one collect two parallel arrays? a = Array.new(12) {|i| i*i} b = Array.new(12) {|i| i*3} def f(n) 1 == n % 2 end def g(a) return false if a.size != 7 0 == a.inject{|p,n| p*n}/a.inject{|s,n| s+n} % 4 end result = [] a.each_with_index{ |e,i| result << (f(e) && g( b[i-3 .. i+3] )) }