Home » Ruby programming

Array.zip() Method with Example in Ruby

Ruby Array.zip() Method: Here, we are going to learn about the Array.zip() Method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 15, 2020

Array.zip() Method

In this article, we will study about Array.zip() Method. You all must be thinking the method must be doing something which is related to zipping values of the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

Method description:

This method is a public instance method and defined for the Array class in Ruby's library. This method works in a way that it converts any argument into the Array object and then merges that Array instance with the elements of self array. This results in a sequence of Array.length n-element Array object where n is possibly more than the number of elements. The method will supply "nil" values if the size of any argument is less than the size of the initial Array. If you are providing a block then the block is called for each resulting Array object otherwise an Array of Arrays is returned or you can say that a multi-dimensional Array is returned.

Syntax:

    array_instance.zip(arg, ...) -> new_ary
    array_instance.zip(arg, ...) { |arr| block } -> nil 

Argument(s) required:

This method can take multiple objects as arguments.

Example 1:

=begin
  Ruby program to demonstrate zip method
=end

# array declaration
table = ["fatima","Sabita","Monica","Syresh","Kamish","Punish"]
table1 = ["Apple","Banana","Orange","Papaya"]

puts "Array zip implementation:"

puts "#{["abc","pqr"].zip(table,table1)}"
puts "Array instance : #{table}"

Output

Array zip implementation:
[["abc", "fatima", "Apple"], ["pqr", "Sabita", "Banana"]]
Array instance : ["fatima", "Sabita", "Monica", "Syresh", "Kamish", "Punish"]

Explanation:

In the above code, you can see that we are zipping the Array object with the help of Array.zip() method. This method is a non-destructive method and does not create any change in the actual Array instance.

Example 2:

=begin
  Ruby program to demonstrate zip method
=end

# array declaration
table = ["fatima","Sabita","Monica","Syresh","Kamish","Punish"]
table1 = ["Apple","Banana","Orange","Papaya"]

puts "Array zip implementation:"

puts "#{table.zip(["Kuber","Kamesh"],table1)}"
puts "Array instance : #{table}"

Output

Array zip implementation:
[["fatima", "Kuber", "Apple"], ["Sabita", "Kamesh", "Banana"], ["Monica", nil, "Orange"], ["Syresh", nil, "Papaya"], ["Kamish", nil, nil], ["Punish", nil, nil]]
Array instance : ["fatima", "Sabita", "Monica", "Syresh", "Kamish", "Punish"]

Explanation:

In the above code, you can see that we are zipping the Array object with the help of the Array.zip() method. This method is a non-destructive method and does not create any change in the actual Array instance.




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.