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 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
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 …
ri Enumerable.zip
--------------------------------------------------------- Enumerable#zip
enum.zip(arg, …) => array
enum.zip(arg, …) {|arr| block } => nil
···
from :0
from :0
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]]
–
Simon Strandgaard