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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540954.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540954.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540954.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

matlab中迪杰斯特拉算法,dijkstra算法(迪杰斯特拉算法)

單源最短路徑算法——Dijkstra算法(迪杰斯特拉算法)一 綜述 Dijkstra算法(迪杰斯特拉算法)主要是用于求解有向圖中單源最短路徑問題.其本質是基于貪心策略的(具體見下文).其基本原理如下: (1)初始化:集合vertex_set初始為{sourc ...Dijkstra【迪杰斯特拉算法…

關于概率算法的問題,不知道邏輯錯在哪里,求debug

做個骰子成功幾率的分析,投n顆骰子,第一次投成功的幾率是a,然后投成功的骰子,需要再投1次,這次成功的幾率是b。第二次成功的骰子才算最終成功。 要分析出n顆骰子,最終成功0到n顆的概率。 我寫了個算法,求出…

tps 交易量_交易處理系統(TPS)

tps 交易量A transaction is a simple process that takes place during business operations. The transaction processing system (TPS) manages the business transactions of the client and therefore helps a companys operations. A TPS registers, as well as all of i…

matlab for循環不覆蓋,將輸出保存到文本文件而不覆蓋和打印矩陣中的N個條目[matlab]...

這是代碼:for i 1:4;fileID fopen(testdata.txt, at);fprintf(fileID, this is answer %d\n,i);fprintf(fileID, %5.3e\n, v{i});fclose(fileID);end在記事本中回答:this is answer 11.000e0001.000e0001.000e0001.000e0001.000e0001.000e0000.000e0001…

(轉)Redis研究(一)—簡介

http://blog.csdn.net/wtyvhreal/article/details/41855327 Redis是一個開源的高性能鍵值對數據庫。它通過提供多種鍵值數據類型來適應不同場景下的存儲需求,并借助許多高層級的接口使其可以勝任如緩存、隊列系統等不同的角色。 1.1歷史和發展 2008年,意…

c bitset get_Java BitSet get()方法與示例

c bitset getBitSet類的get()方法 (BitSet Class get() method) Syntax: 句法: public boolean get(int bit_in);public BitSet get(int st_in, int en_in);get() method is available in java.util package. get()方法在java.util包中可用。 get(int bit_in) meth…

有擾動的閉環傳遞函數 matlab,(d)閉環系統的誤差傳遞函數.PPT

(d)閉環系統的誤差傳遞函數3. 控制系統的方框圖模型 若已知控制系統的方框圖,使用MATLAB函數可實現方框圖轉換。 a).串聯 如圖所示G1(s)和G2(s)相串聯,在MATLAB中可用串聯函數series( )來求G1(s)G2(s),其調用格式為 [num,den]series(num1,den1,num2,den2) 其中: b)并…

CYQ.Data 輕量數據層之路 自定義MDataTable綁定續章(七)

本章起,將續章講解整框架當初的設計思路: 本章既為續章,說明我以前寫過,是的,以前我寫過內部整個MDataTable的構造,不過,當初匆匆寫完后, 最后一步的實現MDataTable綁定GridView/Dat…

php 文字超出畫布,input實現文字超出省略號(代碼示例)

本篇文章給大家帶來的內容是關于input實現文字超出省略號(代碼示例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。input實現文字省略號功能普通元素實現文字超出寬度自動變成省略號非常簡單,給元素加個寬度&…

c++ stl stack_C ++ STL中的stack :: top()函數

c stl stackPrototype: 原型&#xff1a; stack<T> st; //declarationT st.top();Parameter: 參數&#xff1a; No parameter passedReturn type: T //data type 返回類型&#xff1a; T //數據類型 Header file to be included: 包含的頭文件&#xff1a; #include …

排序算法系列:插入排序算法

概述 直接插入排序&#xff08;Straight Insertion Sort&#xff09;的基本操作是將一個記錄插入到已經排好序的有序表中&#xff0c;從而得到一個新的、記錄數增1的有序表。 – 《大話數據結構》 版權說明 著作權歸作者所有。商業轉載請聯系作者獲得授權&#xff0c;非商業轉載…

php點擊復制按鈕到我的粘貼板,js實現點擊復制當前文本到剪貼板功能(兼容所有瀏覽器)...

最近做項目時&#xff0c;在網站框架搭建過程&#xff0c;有一個功能需要實現復制文本到剪貼板&#xff0c;相信這個功能很常用&#xff0c;但是對于不常寫JS代碼的我來說是一個比較大的挑戰&#xff0c;回想以前做過的一個站點&#xff0c;使用window.clipboardData實現復制到…

算法導論 算法_算法導論

算法導論 算法Algorithms are an integral part of the development world. Before starting coding of any software first an effective algorithm is designed to get desired outputs. In this article, we will understand what are algorithms, characteristics of algor…

[Phonegap+Sencha Touch] 移動開發77 Cordova Hot Code Push插件實現自己主動更新App的Web內容...

原文地址&#xff1a;http://blog.csdn.net/lovelyelfpop/article/details/50848524 插件地址&#xff1a;https://github.com/nordnet/cordova-hot-code-push 以下是我對GitHub項目readme的翻譯 ——————————————————————————————————————…

java 如何重寫迭代器,如何用Java按需定制自己的迭代器

編寫自己的迭代器的流程是&#xff1a;首先實現Iterable接口&#xff0c;進而實現該接口中的Iterator iterator()方法&#xff0c;該方法返回接口Iterator&#xff0c;Iterator接口中封裝了next&#xff0c;hasnext&#xff0c;remove等方法。實現了Iterable接口的類能夠通過fo…

count函數里加函數_PHP count()函數與示例

count函數里加函數PHP count()函數 (PHP count() function) "count() function" is used to get the total number of elements of an array. “ count()函數”用于獲取數組元素的總數。 Syntax: 句法&#xff1a; count(array, [count_mode])Here, 這里&#xff0…

php整合支付寶,Thinkphp5.0整合支付寶在線下單

thinkphp5.0支付寶在線支付下單整個流程&#xff0c;包括創建訂單、支付成功回調更新訂單狀態、最終跳轉到商戶訂單詳情頁查看演示下載資源&#xff1a;17次 下載資源下載積分&#xff1a;998積分支付寶在線支付控制器代碼 public function alipay() {//發起支付寶支付$order_n…

python函數示例_PHP closeir()函數與示例

python函數示例PHP Closedir()函數 (PHP closedir() function) The full form of closedir is "Close Directory", the function closedir() is used to close an opened directory. Closedir的完整格式為“ Close Directory” &#xff0c; 函數closedir()用于關閉打…

java宋江,Java編程內功-數據結構與算法「單鏈表」,

package com.structures.linkedlist;public class SingleLinkedListDemo {public static void main(String[] args) {HeroNode heroNode1 new HeroNode(1, "宋江", "及時雨");HeroNode heroNode2 new HeroNode(2, "盧俊義", "玉麒麟"…

智能家居逐漸融入AI技術 向大眾市場擴張仍需時間

雖然智能家居變得越來越普遍&#xff0c;并且大眾認知度越來越高&#xff0c;但是在這一技術變得像智能手機一樣無處不在之前&#xff0c;OEM和服務提供商仍然有很長的路要走。 在2016年11月在硅谷舉行的智能家居峰會上&#xff0c;代表們聽到了來自整個價值鏈上的聲音&#xf…