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.mzph.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里暴雪的“亲儿子”们

. 不知何时,魔兽世界的词汇中忽然出现了一个新玩意:亲儿子。虽说这个称呼现在大多是拿来调侃法爷的,但江山代有儿子出,各领风骚一两天,今天风光无限的法爷们也经历过被其他职业压得抬不起头的小媳妇生涯。那么今天…