Last week I blogged about the Enumerable module in Ruby and specifically mentioned the Enumerable#zip method. I didn’t think the zip method was particularly useful, but I have actually found a few extra features which are very nice.
To refresh your memory, the zip method will combine the elements of two different arrays.
I also mentioned that you can actually combine multiple arrays in this way.
What I didn’t realize was that you can very easily create a hash mapping between two arrays when you use this method in combination with the [] method on the Hash class.
This is a pretty cool trick. Lastly, we can actually pass a block to the zip method – the block will execute exactly the same as an each block executed on the result.
I still don’t really get the name of the method, but it’s pretty useful. Happy coding.