Why does calling zip on an array with a block always return nil
arrays, ruby
Solution
No
For better or for worse, that's just how it works. It either returns the result or yields it to the block; it doesn't do both.
By the way, in your example `a.zip(b)` is actually `[[1, 4], [2, 5], [3, 6]]`.
Problem
So I was trying to take two arrays `a` and `b` return a third array such that the nth element of the third array is the sum of the nth elements of arrays `a` and `b`. I was looking at the `#zip` method which interleaves arrays such that if `a = [1, 2, 3]` and `b = [4, 5, 6]` `a.zip(b) = [[1, 4], [2, 5], [3, 6]]`. ruby-doc.org says If a block is given, it is invoked for each output array... While messing around with it, I found something interesting though. If you call zip with a block, it always seems to return nil. Am I doing something wrong here? ``` c = a.zip(b) { |x| x.reduce(:+) } ``` returns `nil` ``` c = a.zip(b).map { |x| x.reduce(:+) } ``` returns the desired result