From: dblack@... Date: 2002-11-12T22:36:34+09:00 Subject: Re: ruby-dev summary 18613-18710 Hi -- On Tue, 12 Nov 2002, TAKAHASHI Masayoshi wrote: > Hi all, > > One of the happiest things for me in RubyConf2002 was > to meet readers of this summary. Thanks again! It was good to see you again -- looking forward to next year! > [ruby-dev:18651] Enumerable#zip > > Matz implemented Enumerable#zip method, but he has not > decide its behavior yet. > > Enumerable#zip is a method to mix some sequences(Array or > Object which has to_ary method). But, when length of > sequences are not same, the behavior is not clear. [...] > > a = [1,2,3,4] > b = [2,4] > a.zip(b) #=> ? > c.) a.zip(b) #=> [[1,2],[2,4],[3,nil],[4,nil]] > b.zip(a) #=> [[2,1],[4,2]] That makes sense to me. The iteration is over the receiver, so the length of the result is the same as the length of the receiver. Something like: module Enumerable def zip(other) (0...size).map {|i| [to_a[i],other.to_a[i]] } end end (Of course it would look better if we had #map_with_indices :-) I once wrote something called Array#braid, which zipped two or more arrays: [1,2,3].braid([4,5,6],[7,8,9]) # => [ [1,4,7], [2,5,8], [3,6,9] ] Was there any discussion of a generalized version of zip? David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav