日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/11 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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_aryarray_instance.zip(arg, ...) { |arr| block } -> nil

Argument(s) required:

所需參數:

This method can take multiple objects as arguments.

此方法可以將多個對象用作參數。

Example 1:

范例1:

=beginRuby 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:

=beginRuby 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

總結

以上是生活随笔為你收集整理的ruby array_Ruby中带有示例的Array.zip()方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。