From: Charles Comstock Date: 2004-04-12T11:39:17+09:00 Subject: Re: zip block oddities/bug? Simon Strandgaard wrote: > When supplying a block to zip, shouldn't you > be able to affect the output produced by #zip ? > > No matter what I do it outputs 'nil'.. > > Is this the intented behavier ? > > irb(main):020:0> (4..6).zip {|i| p i; nil } > [4] > [5] > [6] > => nil > irb(main):021:0> (4..6).zip {|i| p i; [] } > [4] > [5] > [6] > => nil > irb(main):022:0> (4..6).zip {|i| p i; 666 } > [4] > [5] > [6] > => nil > irb(main):023:0> (4..6).zip(8) {|i| p i; 666 } > TypeError: cannot convert Fixnum into Array > from (irb):23:in `zip' > from (irb):23 > from :0 > irb(main):024:0> (4..6).zip(8) {|i| p i; nil } > TypeError: cannot convert Fixnum into Array > from (irb):24:in `zip' > from (irb):24 > from :0 > irb(main):025:0> (4..6).zip([8]) {|i| p i; nil } > [4, 8] > [5, nil] > [6, nil] > => nil > irb(main):026:0> > > Except from the last line, I only get nil outputted > The block is mentioned in RI, but .. > > > erver> ri Enumerable.zip > --------------------------------------------------------- Enumerable#zip > enum.zip(arg, ...) => array > enum.zip(arg, ...) {|arr| block } => nil > ------------------------------------------------------------------------ > Converts any arguments to arrays, then merges elements of _enum_ > with corresponding elements from each argument. This generates a > sequence of +enum#size+ _n_-element arrays, where _n_ is one more > that the count of arguments. If the size of any arguemnt is less > than +enum#size+, +nil+ values are supplied. If a block given, it > is invoked for each output array, otherwise an array of arrays is > returned. > > a = [ 4, 5, 6 ] > b = [ 7, 8, 9 ] > > (1..3).zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]] > "cat\ndog".zip([1]) #=> [["cat\n", 1], ["dog", nil]] > (1..3).zip #=> [[1], [2], [3]] > > server> > > -- > Simon Strandgaard irb(main):009:0> (1..3).zip([4,5,6]) {|x| p x;} [1, 4] [2, 5] [3, 6] => nil I don't follow why it was decided that the final result be nil, it would be nice if returned the results from the block, meaning the above example would result in: irb(main):009:0> (1..3).zip([4,5,6]) {|x| p x; x} [1, 4] [2, 5] [3, 6] => [[1, 4],[2, 5], [3, 6]] But I guess that was just the original concept. Charles Comstock