walter a kehowski wrote: > "Gene Tani" <gene.tani / gmail.com> wrote in message > news:1123474109.033229.303310 / o13g2000cwo.googlegroups.com... >> can we merge 2 threads >> >> http://groups-beta.google.com/group/comp.lang.ruby/browse_frm/thread/baeb5e192b809bf3/608d3656142aeba6?lnk=st&q=lucky+stiff+group:comp.lang.ruby&rnum=6&hl=en#608d3656142aeba6 >> > > Here's the nugget embedded in the thread: > > a=[1,2,3] > b=[4,5,6] > > class Array > > def cartprod(b) > > self.inject(a=[]){|a,x| b.inject(a){|a,y| a << [x,y]}} > > end > > end > > p a.cartprod(b) > > Very nice! This is why we like Ruby! [IMHO] > > Walter Kehowski You can do it a bit shorter. Note that you don't need the "a=[]" and the "self.": aa=[1,2,3] bb=[4,5,6] class Array def cartprod(b) self.inject(a=[]){|a,x| b.inject(a){|a,y| a << [x,y]}} end def cartprod2(b) inject([]){|a,x| b.inject(a){|a,y| a << [x,y]}} end end p aa.cartprod(bb) p aa.cartprod2(bb) Kind regards robert