×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby Array.zip() Method

By IncludeHelp Last updated : December 01, 2024

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.

Description and Usage

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 

Parameters

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.