ruby array_Ruby中带有示例的Array.select方法

ruby array

Array.select方法 (Array.select Method)

In the last articles, we have seen how to iterate over the instances of Array class? We have seen that we have got methods like Array.each, Array.reverse_each and Array.map for this purpose. In this article, we will learn about the implementation of Array.select.

在上一篇文章中,我们已经看到了如何遍历Array类的实例? 我们已经看到,为此目的,我们已经有了Array.each , Array.reverse_each和Array.map之类的方法。 在本文中,我们将学习Array.select的实现。

Array.select method, as the name suggests, is used to select some elements from the Array. This method is non-destructive and does not bring any change in the actual values of the Array object. This method works based on certain conditions which you will provide inside the pair of parentheses. This method is based on the criteria you provide inside the block. This method will not work if you do not specify any conditions inside the block. Though it will not throw an exception you will get nil as the result. So, if you are working with this method you should be having certain conditions based on which the element is going to be selected from the object of Array class. If you want to print all the elements of Array instance then you can go for Array.each method and avoid going for this one.

顾名思义, Array.select方法用于从Array中选择一些元素。 此方法是非破坏性的,不会对Array对象的实际值带来任何变化。 该方法根据您将在圆括号内提供的某些条件来工作。 此方法基于您在块内提供的条件。 如果未在块内指定任何条件,则此方法将不起作用。 尽管它不会引发异常,但您将得到nil作为结果。 因此,如果使用此方法,则应具有某些条件,根据这些条件要从Array类的对象中选择元素。 如果要打印Array实例的所有元素,则可以使用Array.each方法,并避免使用该方法 。

Syntax:

句法:

    Array.select{|var| #condition}

Parameter(s):

参数:

This method does not permit the passing of any arguments instead it mandates a condition.

此方法不允许传递任何参数,而是要求一个条件。

Example 1:

范例1:

=begin
Ruby program to demonstrate Array.select
=end
# array declaration
num = [2,44,2,5,7,83,5,67,12,11,90,78,9]
puts "Enter 'a' for Even numbers and 'b' for odd numbers"
opt = gets.chomp
if opt == 'a'
puts "Even numbers are:"
puts num.select{|num|
num%2 == 0
}
elsif opt == 'b'
puts "Odd numbers are:"
puts num.select{|num|
num%2 !=0
}
else
puts "Wrong selection. Input valid option"
end

Output

输出量

RUN 1:
Enter 'a' for Even numbers and 'b' for odd numbers
a
Even numbers are:
2
44
2
12
90
78
RUN 2:
Enter 'a' for Even numbers and 'b' for odd numbers
b
Odd numbers are:
5
7
83
5
67
11
9

Explanation:

说明:

In the above code, you can observe that we are taking input from the user about what type of numbers the user wants as the output. This is because we want to pass certain conditions inside the Array.select method. We are giving the response to the user as per the option provided by the user and this method is used in this way only.

在上面的代码中,您可以观察到我们正在从用户那里获取用户想要输入什么类型的数字作为输入。 这是因为我们要在Array.select方法内部传递某些条件。 我们根据用户提供的选项向用户提供响应,并且仅以这种方式使用此方法。

Example 2:

范例2:

=begin
Ruby program to demonstrate Array.select
=end
# array declaration
num = [2,44,2,5,7,83,5,67,12,11,90,78,9]
puts num.select{|a|}

Output

输出量

    # no o/p

Explanation:

说明:

In the above output, you can observe that when you are not specifying any condition inside the method, then you are not getting anything as the output.

在上面的输出中,您可以观察到,当您未在方法内部指定任何条件时,则不会得到任何输出。

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

ruby array

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377981.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

十七、轮廓发现

一、轮廓发现原理 轮廓发现是在图像边缘提取的基础上寻找对象轮廓的方法,故边缘提取的阈值的选定会影响到最终轮廓发现的结果。 其本质是基于二值图像的,边缘提取常用Canny进行提取边缘 轮廓发现也是基于拓扑结构,扫描连通图,最后…

关于 WebRequest.RegisterPrefix

RegisterPrefix 方法将 WebRequest 子代注册到服务请求。 WebRequest 后代通常被注册来处理特定的协议(例如 HTTP 或 FTP),但也可能被注册来处理对特定服务器或服务器上的路径的请求。 已注册的预注册保留类型包括下列类型: htt…

LeetCode 404. 左叶子之和思考分析

题目 计算给定二叉树的所有左叶子之和。 如果是下面的树,只有一个左叶子结点4 思考分析 由此我们可以得到左叶子结点的定义: cur->left !NULL && cur->left->leftNULL && cur->left->rightNULL 递归遍历累积操作 …

天王盖地虎

1&#xff0c;求有序数列中某个元素的个数 思想&#xff1a;二分找上下界&#xff1a; int element_count(int * set, int len, int e) {int f, a, b, t;for(a 0, b len - 1; a < b; set[t a b >> 1] < e ? (a t 1) : (b t - 1));for(f a, b len - 1; a…

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

ruby arrayArray.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…

十八、对已经找到轮廓的图像进行测量

图像轮廓的获取可参考博文十七 一、相关原理 1&#xff0c;弧长和面积 对于弧长和面积&#xff0c;计算出来的轮廓单位都是像素 2&#xff0c;多边形拟合 每一个轮廓都是一系列的点&#xff0c;然后通过多边形进行拟合&#xff0c;无限的接近真实形状 相关API&#xff1a;…

Linux 终端登录SSH:解决SSH的Access Denied 和 make xconfig的使用

对于SSH&#xff1a; 可以不是用putty&#xff0c;尤其是putty出现 Access denied 的时候&#xff0c;请换用konsole 或者其他终端 &#xff08;如 terminal&#xff09;在command line中&#xff1a; # ssh 要访问的主机ip eg &#xff1a; ssh 192.168.111 然后确定…

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

题目 给定一个二叉树&#xff0c;在树的最后一行找到最左边的值。 递归解 左下角要满足两个条件&#xff1a; 1、深度最大的叶子结点 2、最左结点&#xff1a;使用前序遍历&#xff0c;优先左边搜索。 1、确定递归函数的参数和返回值 参数&#xff1a;树的根结点&#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提供了一些内置函数&#xff0c;这些函数用于验…

Linux 平台下 MySQL 5.5 安装 说明 与 示例

一.下载说明前期的一些准备说明&#xff0c;参考&#xff1a;MySQL 发展史http://blog.csdn.net/tianlesoftware/article/details/6999245Mysql 不同版本 说明http://blog.csdn.net/tianlesoftware/article/details/6723117 MySQL 分为Community Server 和 Enterprise Edition。…

开始python之旅

接触python缘于工作所需&#xff0c;曾经接触过C、C等语言&#xff0c;对于编程语言在学习上大体是一个套路&#xff0c;当然套路因人而异&#xff0c;适合就好。接下来&#xff0c;我将不断分享python的知识和学习技巧&#xff0c;共同学习。 起源 初识一门语言善于先了解语言…

LeetCode 112. 路径总和 、113. 路径总和 II 思考分析

目录112. 路径总和题目递归解递归解&#xff0c;其他人的解法迭代解&#xff0c;其他人的解法113. 路径总和 II题目递归解递归解&#xff0c;参考别人的思路112. 路径总和 题目 给定一个二叉树和一个目标和&#xff0c;判断该树中是否存在根节点到叶子节点的路径&#xff0c;…

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 分片集群不会有单点失效。 本章节描述了集群服务器中可能出现的故障&#xff0c;及相应的对策。 1. 某个mongos路由进程故障 每一个mongos会运行每一台应用服务器上面&#xff0c;该应用服务器只能通过这个mongos进程和集群进行通信。mongos进程不是…

看张子阳的书真是收获很多,也醒悟了很多(一)

摘录&#xff1a; 这是有一次开会时&#xff0c;我的老总跟我们说了这样一个事例&#xff1a;通常来说&#xff0c;医生是很高尚的职业&#xff08;暂不考虑国内医生的负面新闻&#xff09;&#xff0c;尤其是牙科医生&#xff0c; 他们有着体面的工作并且收入不菲。但是&#…

【C++ grammar】抽象、封装与this指针

目录1、Abstraction and Encapsulation&#xff08;抽象与封装&#xff09;1. Data Field Encapsulation (数据域封装)2. Accessor and Mutator (访问器与更改器)2.1. To read/write private data, we need get/set function (为读写私有数据&#xff0c;需要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()…

十九、图像的形态学操作

一、图像形态学 图像形态学是图像处理学科的一个单独分支学科 主要针对的是灰度图和二值图像 是由数学的集合论以及数学中的拓扑几何原理发展而来 二、膨胀操作&#xff08;dilate&#xff09; 33的卷积核 以33为卷积核从左往右(从上往下)开始运行&#xff0c;若这卷积核…

X名称空间(WPF)

笔记简述 闲话x名称空间简要x名称空间的Attributex名称空间的标签扩展x名称空间的XAML指令元素闲话 本笔记参考与《深入浅出WPF》、MSDN、Some Blog… MSDN的飞机票点这里。 x名称空间简要 在VS中新建个WpfApplication都会自动生成xmlns:x"http://schemas.microsoft.com/w…