Ruby provides lots of built-in methods for working with arrays, but at first glance, some seem to be missing from the Array documentation. A good example is the
#find method, which returns the first element satisfying the criteria you provide in a block.
The fact is, these methods are mixed in from the Enumerable module, a collection of useful methods that can be applied to Arrays, Ranges, and Sets, among other Ruby classes. A simple check
whatever_object.is_a? Enumerable will confirm whether whatever object your dealing with includes the Enumerable module.
One of these methods,
#zip, has been calling out to me since I started learning Ruby. It seemed like an alchemical process that merged arrays in a mysterious way. In practice, it simply merges the corresponding elements of each array, returning an array of arrays.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Pretty nice, but why don’t we do Tom Waits proud and join those phrases?
#zip a block will invoke the block for each output array, but return
nil at the end of the day. A call to
#map to will do the trick.
1 2 3 4 5
#zip can merge any number of arrays…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Not all the tastiest combinations, but that’s how
#zip works, it just matches up the elements in whatever order they appeared in the original arrays.