ruby array_Ruby中帶有示例的Array.cycle()方法

ruby array

Array.cycle()方法 (Array.cycle() Method)

In this article, we will study about Array.cycle() method. You must be a little more excited to read about Array.cycle method due to its catchy name as I was pretty amazed after reading this method. In the upcoming content, we will see the way through which we can implement this method. We will understand it with the help of its syntax and demonstrating examples.

在本文中,我們將研究Array.cycle()方法 。 由于Array.cycle方法具有易記的名稱,因此您必須更加興奮地閱讀Array.cycle方法,因為閱讀此方法后我感到非常驚訝。 在接下來的內容中,我們將看到實現此方法的方式。 我們將借助其語法和演示示例來理解它。

Method description:

方法說明:

This method is specially defined for the Array class in Ruby's library. This method is one of the examples of Array instance methods. It works like a loop and calls or invokes the given block for each element of Array instance for n number of times. This method will not work if you do not provide any positive number. If you are not providing anything then it may result in the infinite repetition of the elements of the object of Array class. The return type of this method is "nil" and it does not undergo any change in the actual elements of Array objects. This method can be considered as one of the examples of non-destructive methods present in the Rich Ruby library.

此方法是為Ruby庫中的Array類專門定義的。 此方法是Array實例方法的示例之一。 它像循環一樣工作,并為Array實例的每個元素調用或調用給定的塊n次。 如果您不提供任何正數,則此方法將不起作用。 如果您不提供任何內容,則可能導致Array類對象的元素無限重復。 此方法的返回類型為“ nil”,并且在Array對象的實際元素中未進行任何更改。 該方法可以視為Rich Ruby庫中存在的非破壞性方法的示例之一。

Syntax:

句法:

    cycle(n=nil) { |obj| block } -> nil

Argument(s) required:

所需參數:

This method requires one positive number and if nothing is passed then you have to encounter an infinite loop.

此方法需要一個正數,如果不進行任何傳遞,則必須遇到無限循環。

Example 1:

范例1:

=begin
Ruby program to demonstrate cycle method
=end
# array declaration
array1 = ["1","Ramesh","Apple","12","Sana","Yogita","Satyam","Harish"]
puts "Array cycle implementation."
array1.cycle(3){|x| puts  x}

Output

輸出量

Array cycle implementation.
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish

Explanation:

說明:

In the above code, you will observe that each element of the Array instance have been repeated three times and the repetition is not random, it is going in a proper sequence as stored inside the instance of Array class with which the method is invoked.

在上面的代碼中,您將觀察到Array實例的每個元素已經重復了3次,并且重復不是隨機的,它按照正確的順序存儲在調用該方法的Array類實例中。

Example 2:

范例2:

=begin
Ruby program to demonstrate cycle method
=end
# array declaration
array1 = ["1","Ramesh","Apple","12","Sana","Yogita","Satyam","Harish"]
puts "Array cycle implementation."
array1.cycle{|x| puts x}

Output

輸出量

Array cycle implementation.
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish
1
Ramesh
Apple
12
Sana
Yogita
Satyam
Harish
1
Ramesh
Apple
12
.
.
.
.
Infinite loop...

Explanation:

說明:

When you will run the above code, you will observe that it will result in an infinite loop. This has happened because you are not passing any argument or positive number with the method.

當您運行上述代碼時,您會發現它將導致無限循環。 發生這種情況是因為您沒有使用該方法傳遞任何參數或正數。

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

ruby array

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

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

相關文章

十八、對已經找到輪廓的圖像進行測量

圖像輪廓的獲取可參考博文十七 一、相關原理 1,弧長和面積 對于弧長和面積,計算出來的輪廓單位都是像素 2,多邊形擬合 每一個輪廓都是一系列的點,然后通過多邊形進行擬合,無限的接近真實形狀 相關API:…

Linux 終端登錄SSH:解決SSH的Access Denied 和 make xconfig的使用

對于SSH: 可以不是用putty,尤其是putty出現 Access denied 的時候,請換用konsole 或者其他終端 (如 terminal)在command line中: # ssh 要訪問的主機ip eg : ssh 192.168.111 然后確定…

LeetCode 513. 找樹左下角的值 思考分析

題目 給定一個二叉樹,在樹的最后一行找到最左邊的值。 遞歸解 左下角要滿足兩個條件: 1、深度最大的葉子結點 2、最左結點:使用前序遍歷,優先左邊搜索。 1、確定遞歸函數的參數和返回值 參數:樹的根結點&#xff…

利用MyBatis的動態SQL特性抽象統一SQL查詢接口

1. SQL查詢的統一抽象 MyBatis制動動態SQL的構造,利用動態SQL和自定義的參數Bean抽象,可以將絕大部分SQL查詢抽象為一個統一接口,查詢參數使用一個自定義bean繼承Map,使用映射的方法構造多查詢參數.在遇到多屬性參數(例如order by,其參數包括列名,升序降序類型,以及可以多個列及…

ctype函數_PHP Ctype(字符類型)函數

ctype函數Ctype功能 (Ctype functions) PHP provides some of the built-in functions, which are used to verify the characters in the string i.e. to check whether a string contains the characters of specific types. PHP提供了一些內置函數,這些函數用于驗…

Linux 平臺下 MySQL 5.5 安裝 說明 與 示例

一.下載說明前期的一些準備說明,參考:MySQL 發展史http://blog.csdn.net/tianlesoftware/article/details/6999245Mysql 不同版本 說明http://blog.csdn.net/tianlesoftware/article/details/6723117 MySQL 分為Community Server 和 Enterprise Edition。…

開始python之旅

接觸python緣于工作所需,曾經接觸過C、C等語言,對于編程語言在學習上大體是一個套路,當然套路因人而異,適合就好。接下來,我將不斷分享python的知識和學習技巧,共同學習。 起源 初識一門語言善于先了解語言…

LeetCode 112. 路徑總和 、113. 路徑總和 II 思考分析

目錄112. 路徑總和題目遞歸解遞歸解,其他人的解法迭代解,其他人的解法113. 路徑總和 II題目遞歸解遞歸解,參考別人的思路112. 路徑總和 題目 給定一個二叉樹和一個目標和,判斷該樹中是否存在根節點到葉子節點的路徑,…

kotlin 查找id_Kotlin程序查找矩陣的轉置

kotlin 查找idA transpose of a matrix is simply a flipped version of the original matrix. We can transpose a matrix by switching its rows with its columns 矩陣的轉置只是原始矩陣的翻轉形式。 我們可以通過切換矩陣的行和列來轉置矩陣 Given a matrix, we have to…

[mongodb翻譯]分片和故障轉移

一個配置恰當的mongodb 分片集群不會有單點失效。 本章節描述了集群服務器中可能出現的故障,及相應的對策。 1. 某個mongos路由進程故障 每一個mongos會運行每一臺應用服務器上面,該應用服務器只能通過這個mongos進程和集群進行通信。mongos進程不是…

看張子陽的書真是收獲很多,也醒悟了很多(一)

摘錄: 這是有一次開會時,我的老總跟我們說了這樣一個事例:通常來說,醫生是很高尚的職業(暫不考慮國內醫生的負面新聞),尤其是牙科醫生, 他們有著體面的工作并且收入不菲。但是&#…

【C++ grammar】抽象、封裝與this指針

目錄1、Abstraction and Encapsulation(抽象與封裝)1. Data Field Encapsulation (數據域封裝)2. Accessor and Mutator (訪問器與更改器)2.1. To read/write private data, we need get/set function (為讀寫私有數據,需要get/set函數)2.2. …

java創建臨時文件_用Java創建一個臨時文件

java創建臨時文件The task is to create a temporary file in Java. 任務是用Java創建一個臨時文件。 Creating a temporary file 創建一個臨時文件 To create a temporary file in java – we use createTempFile() method of "File" class. The createTempFile()…

十九、圖像的形態學操作

一、圖像形態學 圖像形態學是圖像處理學科的一個單獨分支學科 主要針對的是灰度圖和二值圖像 是由數學的集合論以及數學中的拓撲幾何原理發展而來 二、膨脹操作(dilate) 33的卷積核 以33為卷積核從左往右(從上往下)開始運行,若這卷積核…

X名稱空間(WPF)

筆記簡述 閑話x名稱空間簡要x名稱空間的Attributex名稱空間的標簽擴展x名稱空間的XAML指令元素閑話 本筆記參考與《深入淺出WPF》、MSDN、Some Blog… MSDN的飛機票點這里。 x名稱空間簡要 在VS中新建個WpfApplication都會自動生成xmlns:x"http://schemas.microsoft.com/w…

基于Bresenham和DDA算法畫線段

直線:ykxb 為了將他在顯示屏上顯示出來,我們需要為相應的點賦值,那么考慮到計算機的乘法執行效率,我們肯定不會選擇用Ykxb這個表達式求值,然后進行畫線段。 我們應當是將它轉化為加法運算。 下面提供兩種常見的算法&am…

leetcode 106. 從中序與后序遍歷序列構造二叉樹 105. 從前序與中序遍歷序列構造二叉樹思考分析

目錄1、106題目2、參考思路:遞歸切割數組3、105題目4、同樣思路的代碼1、106題目 2、參考思路:遞歸切割數組 代碼參考:公眾號:代碼隨想錄 后序數組中序數組 以 后序數組(左右中)的最后一個元素作為切割點,先切中序數組…

按頻率對元素進行排序

Prerequisite: 先決條件: Hashing data structure 散列數據結構 How to write user-defined comparator for priority queue STL in C? 如何在C 中為優先級隊列STL編寫用戶定義的比較器? How to sort a map based on values instead of value? 如何根…

二十、分水嶺算法

一、基本原理 分水嶺算法主要是基于距離變換(distance transform),找到mark一些種子點,從這些種子點出發根據像素梯度變化進行尋找邊緣并標記 分水嶺:可以簡單的理解成一座山,然后來洪水了,水開…

細數WOW里暴雪的“親兒子”們

. 不知何時,魔獸世界的詞匯中忽然出現了一個新玩意:親兒子。雖說這個稱呼現在大多是拿來調侃法爺的,但江山代有兒子出,各領風騷一兩天,今天風光無限的法爺們也經歷過被其他職業壓得抬不起頭的小媳婦生涯。那么今天…