In Ruby, zip
is a way to combine elements from two different arrays, albeit in a way that is slightly difficult to understand at first glance.
The documentation is a bit opaque, at least to my eyes, and the examples given take a bit of time to get your head around.
Let’s say you had an array of fruits that you wanted to distribute to your friends. You’re organised, so you have a list of your friends as well.
fruits = ["mango", "orange", "pomegranate"]
friends = ["Rob", "Mary", "Holly"]
Using multiple methods and loops, it’d be fairly trivial to conjure up something to combine these two into a new array, but luckily for us .zip exists to save the day.
friends.zip(fruits)
will return:
[["Rob", "mango"], ["Mary", "orange"], ["Holly", "pomegranate"]]
This way everyone will know what fruit they’re getting.
Note that if one of the two arrays is longer / shorter than the other, the missing space(s) will be filled with nil
values.