ruby array_Ruby中带有示例的Array.zip()方法

ruby array

Array.zip()方法 (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.

在本文中,我们将研究Array.zip()方法 。 你们都必须认为该方法必须做的事情与Array实例的压缩值有关。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

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.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是将任何参数转换为Array对象,然后将该Array实例与self数组的元素合并。 这将导致Array.length个 n元素Array对象的序列,其中n可能大于元素数。 如果任何参数的大小小于初始Array的大小,则该方法将提供“ nil”值。 如果要提供一个块,则为每个结果Array对象调用该块,否则将返回一个Array数组,或者可以说返回了一个多维Array。

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:

范例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.

在上面的代码中,您可以看到我们正在借助Array.zip()方法压缩Array对象。 此方法是一种非破坏性方法,不会在实际的Array实例中创建任何更改。

Example 2:

范例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.

在上面的代码中,您可以看到我们在Array.zip()方法的帮助下压缩Array对象。 此方法是一种非破坏性方法,不会在实际的Array实例中创建任何更改。

翻译自: https://www.includehelp.com/ruby/array-zip-method-with-example.aspx

ruby array

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值