walter a kehowski <wkehowski / cox.net> wrote: > "Giovanni Intini" <intinig / gmail.com> wrote in message > news:a32174e30508070301673ce4d1 / mail.gmail.com... > Remember that in Ruby you seldom have standalone functions. You should > probably create a class that has the cartprod method. > > Giovanni, > > Here's what I have so far: > > def cartprod(a,b) > > c=[] > > a.each do |ae| > b.each do |be| > c << [ae, be] > end > end > > return c > > end > > a=[1,2,3] > > b=[4,5,6] > > c=cartprod(a,b) > > c.each { |ce| print ce,"\n" } > > and this does print all the elements of c. Great. Now how do I do a > class? Can I create a new method for Array? > > Walter Kehowski Note that it might be more memory efficient to write a iteration method: def cartprod(a,b) a.each {|ae| b.each {|be| yield ae, be}} end Then you can also do this if needed c=[] cartprod(a,b) {|x,y| c << [x,y]} If you just need every combination once the iteration approach is more efficient. Kind regards robert