Ruby | Array zip() function
Array#zip() : zip() is a Array class method which Converts any arguments to arrays, then merges elements of self with corresponding elements from each argument.
Syntax: Array.zip()
Parameter: Array
Return: merges elements of self with corresponding elements from each argument.
Example #1 :
# Ruby code for zip() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, 50, 6] # zip method example puts "zip() method form : #{a.zip(b)}\n\n" puts "zip() method form : #{b.zip(c)}\n\n" puts "zip() method form : #{c.zip(c)}\n\n" |
chevron_right
filter_none
Output :
zip() method form : [[18, 1], [22, 4], [33, 1], [nil, 1], [5, 88], [6, 9]] zip() method form : [[1, 18], [4, 22], [1, 50], [1, 6], [88, nil], [9, nil]] zip() method form : [[18, 18], [22, 22], [50, 50], [6, 6]]
Example #2 :
# Ruby code for zip() method # declaring array a = ["abc", "nil", "dog"] # declaring array c = ["cat", nil] # declaring array b = ["cow", nil, "dog"] # zip method example puts "zip() method form : #{a.zip(b)}\n\n" puts "zip() method form : #{b.zip(c)}\n\n" puts "zip() method form : #{c.zip(c)}\n\n" |
chevron_right
filter_none
Output :
zip() method form : [["abc", "cow"], ["nil", nil], ["dog", "dog"]] zip() method form : [["cow", "cat"], [nil, nil], ["dog", nil]] zip() method form : [["cat", "cat"], [nil, nil]]
Recommended Posts:
- Ruby | Array map() function
- Ruby | Array take() function
- Ruby | Array min() function
- Ruby | Array max() function
- Ruby | Array one?() function
- Ruby | Array <=> function
- Ruby | Array &() function
- Ruby Array () function with example
- Ruby | Array pop() function
- Ruby | Array none?() function
- Ruby | Array reject() function
- Ruby | Array repeated_combination() function
- Ruby | Array unshift() function
- Ruby | Array reject!() function
- Ruby | Array assoc() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

