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

ruby array

Ruby Array.keep_if方法 (Ruby Array.keep_if Method)

In the last articles, we have studied the Array methods namely Array.select, Array.reject and Array.drop_while, all these methods are non–destructive methods which means that they do not impose any changes in the actual values of elements residing in the Array instance. If you want to make the above methods destructive, you can add an “!” after the method name. For instance, Array.select! is the destructive version of Array.select. We have also learned about Array.delete_if the method which is already destructive by nature.

在上一篇文章中,我们研究了Array方法,即Array.select , Array.reject和Array.drop_while ,所有这些方法都是非破坏性的方法,这意味着它们不对驻留在数组中的元素的实际值施加任何更改。数组实例。 如果要使上述方法具有破坏性,则可以添加“!” 方法名称之后。 例如,Array.select! 是Array.select的破坏性版本。 我们还了解了Array.delete_if该方法本质上已经具有破坏性。

In this article, we will learn about the Array method Array.keep_if which is also destructive by nature like the delete_if method.

在本文中,我们将学习Array方法Array.keep_if ,它与delete_if方法一样也具有破坏性。

Method description:

方法说明:

This method is used to select elements from the instance of the Array class at the same instant. This method is just opposite of Array.delete_if method because Array.delete_if method deletes the elements from the Array which do not satisfy the condition provided inside the block on the other hand Array.keep_if method keeps or saves the elements which satisfy the condition specified inside the block of the method. This method will remove all the elements from the Array instance if you do not specify or provide any condition inside the block.

此方法用于同时从Array类的实例中选择元素。 此方法与Array.delete_if方法恰好相反,因为Array.delete_if方法从Array中删除不满足块内部提供的条件的元素,而Array.keep_if方法则保留或保存满足内部指定条件的元素方法的块。 如果您未在块内指定或提供任何条件,则此方法将从Array实例中删除所有元素。

Syntax:

句法:

    Array.keep_if{|var|#condition}

Parameter(s): This method does not accept any arguments instead it requires a Boolean condition for operation.

参数 :此方法不接受任何参数,而是需要布尔条件进行操作。

Example 1:

范例1:

=begin
Ruby program to demonstrate Array.keep_if
=end
# array declaration
num = [1,2,3,4,5,6,7,8,9,10,23,11,33,55,66,12]
# user input
puts "Enter the your choice (a)keep even numbers (b) keep odd numbers"
lm = gets.chomp
if lm == 'a'
puts "Even numbers are:"
print num.keep_if { |a| a % 2 ==0 }
elsif lm == 'b'
puts "Odd numbers are:"
print num.keep_if { |a| a % 2 !=0 }
else
puts "Invalid Input"
end

Output

输出量

RUN 1:
Enter the your choice (a)keep even numbers (b) keep odd numbers
a
Even numbers are:
[2, 4, 6, 8, 10, 66, 12]
RUN 2:
Enter the your choice (a)keep even numbers (b) keep odd numbers
b
Odd numbers are:
[1, 3, 5, 7, 9, 23, 11, 33, 55]

Explanation:

说明:

In the above code, you can observe that the method is keeping all the elements that are satisfying the condition provided inside the block of Array.keep_if method. If the user is asking to keep odd numbers, then output is shown in RUN 2 and when the user is asking for even numbers, the output is shown in RUN 1.

在上面的代码中,您可以观察到该方法将保留所有满足Array.keep_if方法块中提供的条件的元素。 如果用户要求保留奇数,则在RUN 2中显示输出,而当用户要求偶数时,在RUN 1中显示输出。

Example 2:

范例2:

=begin
Ruby program to demonstrate Array.keep_if
=end
# array declaration
num = [1,2,3,4,5,6,7,8,9,10,23,11,33,55,66,12]
print num.keep_if{|a|}
puts ""
print num

Output

输出量

[]
[]

Explanation:

说明:

In the above code, you can observe that if you are not specifying any condition inside the method block, then it is deleting or removing all the elements from the Array instance.

在上面的代码中,您可以观察到,如果您未在方法块内指定任何条件,那么它将在Array实例中删除或删除所有元素。

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

ruby array

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

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

相关文章

[实战]MVC5+EF6+MySql企业网盘实战(2)——用户注册

写在前面 上篇文章简单介绍了项目的结构,这篇文章将实现用户的注册。当然关于漂亮的ui,这在追后再去添加了,先将功能实现。也许代码中有不合适的地方,也只有在之后慢慢去优化了。 系列文章 [EF]vs15ef6mysql code first方式 [实战…

Calico IP_AUTODETECTION_METHOD

在 Calico 中,IP_AUTODETECTION_METHOD 的配置项用于指定 Calico 如何检测容器的 IP 地址。 一、kubernetes-internal-ip模式 其中,kubernetes-internal-ip 是一种特殊的模式,用于在 Kubernetes 环境中检测容器的 IP 地址。具体作用如下&…

下个十年高性能 JSON 库来了:fastjson2!

作者 | 磊哥来源 | Java中文社群(ID:javacn666)转载请联系授权(微信ID:GG_Stone)fastjson2 是 fastjson 项目的重要升级,目标是为下一个十年提供一个高性能的 JSON 库,同一套 API 支…

ascii非打印控制字符表_C程序打印ASCII表/图表

ascii非打印控制字符表什么是ASCII码? (What are ASCII Codes?) ASCII stands for American Standard Code for Information Interchange; it is a character encoding standards for information interchange in electronics communication. Each alphabets, spec…

THEOS的第一个TWeak的成功创建

THEOS的第一个TWeak的成功创建THEOS的第一个TWeak的成功创建参考资料:成功的创建一个TWeak的弹出步骤1:安装Xcode和Xcode command line步骤2:安装theosa:下载theos前,设置保存的路径:环境变量b:下载theosc:下载头文件d:下载ldid签名工具e:配置MoblieSubstrate环境f:安装dpkg步骤…

查询中,有没有可能多个索引一起用呢?

其实我们之前所讲的回表,就是两个索引树同时使用,先在二级索引树中搜索到对应的主键值,然后在再去主键索引树中查询完整的记录。但是我今天的问题是,两个不同的二级索引树,会同时生效吗?理论上来说&#xf…

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

ruby arrayArray.sample()方法 (Array.sample() Method) In this article, we will study about Array.sample() method. You all must be thinking the method must be doing something which is quite different from all those methods which we have studied. It is not as…

ThreadLocal夺命11连问

前言前一段时间,有同事使用ThreadLocal踩坑了,正好引起了我的兴趣。所以近期,我抽空把ThreadLocal的源码再研究了一下,越看越有意思,发现里面的东西还真不少。我把精华浓缩了一下,汇集成了下面11个问题&…

关于静态库、动态库的区别汇总

real framework中不可以使用类别 或 不可以不包含类文件real framework 中直接调用NSClassFromString函数会返回null 需要强制加载指定类 或 直接通过类名引用linux中静态库和动态库的区别一、不同库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行…

PHP array_pad()函数与示例

PHP array_pad()函数 (PHP array_pad() function) array_pad() function is used to pad an array to given size with a specified value and returns a new array with a specified value. array_pad()函数用于将数组填充到具有指定值的给定大小,并返回具有指定值…

Spring Boot 优雅配置多数据源

大约在19年的这个时候,老同事公司在做医疗系统,需要和HIS系统对接一些信息,比如患者、医护、医嘱、科室等信息。但是起初并不知道如何与HIS无缝对接,于是向我取经。最终经过讨论采用了视图对接的方式,大致就是HIS系统提…

(转)新ITC提交APP常见问题与解决方法(Icon Alpha,Build version,AppIcon120x120)(2014-11-17)...

1)ICON无法上传,提示图片透明(有Alpha通道)苹果现在不接受png里的Alpha了,提交的图标带有Alpha通道就提示:简单处理:用自带的预览打开,导出时不勾选Alpha,仍保存为png格式…

python 向量取整数_随机整数向量| 使用Python的线性代数

python 向量取整数Prerequisite: 先决条件: Defining a Vector using list 使用列表定义向量 Defining Vector using Numpy 使用Numpy定义向量 Random Integer Vector is a type of vector having a random integer value at each entry. Such types of vectors ha…

Spring 夺命 35 问!

有人说,“Java程序员都是Spring程序员”,可以看出Spring在Java世界里举足轻重的作用。基础1.Spring是什么?特性?有哪些模块?Spring Logo一句话概括:Spring 是一个轻量级、非入侵式的控制反转 (IoC) 和面向切…

Android百度地图开发03之地图控制 + 定位

前两篇关于百度地图的blog写的是,一些基本图层的展示 和 覆盖物的添加地理编码和反地理编码。 接下来,这篇blog主要说一些关于地图控制方面的内容和定位功能。 百度地图提供的关于地图的操作主要有:单击、双击、长按、缩放、旋转、俯视等。 地…

软件工程需要学什么_为什么我们需要软件工程?

软件工程需要学什么Software engineering is the application of the set of pre-defined procedures while developing any project. But why do we need Software engineering? What factors made us implement these predefined set of procedures and protocols while dev…

IDEA 版 Postman 面世了,功能真心强大!

IDEA是最常用的开发工具,很多程序员都想把它打造成一站式开发平台,于是安装了各种各样的插件。最近发现了一款IDEA插件RestfulFastRequest,细节做的真心不错,说它是IDEA版的Postman也不为过,推荐给大家!Res…

DNS子域授权

转载于:https://blog.51cto.com/changeflyhigh/1697257

mongo数据库插入数据_深入研究Mongo数据库

mongo数据库插入数据More popularly known as "mongoDB". It is a no-sql based database. 俗称“ mongoDB” 。 这是一个基于无SQL的数据库。 BASIC STRUCTURE OF MONGO DB MONGO DB的基本结构 A COLLECTION IN MONGODB having 3 DOCUMENTS MONGODB中有3个文档的集…

java方法重载和重载方法_我们可以在Java中重载main()方法吗?

java方法重载和重载方法The question is that "can we overload main() method in Java?" 问题是“我们可以在Java中重载main()方法吗?” Yes, We can overload the main() method in Java. 是的,我们可以重载Java中的main()方法 。 JVM cal…