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