ruby中、.reject_Ruby中带有示例的Array.reject方法

ruby中、.reject

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

In the last article, we have seen how we can make use of the Array.select method in order to print the Array elements based on certain conditions provided inside the block? In this article, we will see how we can make use of the Array.reject method? Array.reject method is totally opposite of the Array.select method.

在上一篇文章中,我们看到了如何利用Array.select方法来基于块内提供的某些条件来打印Array元素? 在本文中,我们将看到如何利用Array.reject方法Array.reject方法与Array.select方法完全相反。

Array.reject method, as the name suggests, is used to reject some elements from the Array. The elements will not be printed if they are satisfying the condition or criteria provided inside the block. This method is a non-destructive method and does not bring any change in the actual values of the Array object. This method works on the basis of certain conditions which you will provide inside the pair of parentheses. This method is totally based on the criteria you provide inside the block. This method will work even if you do not specify any conditions inside the block. It will print all the values if you are not providing any condition or criteria of rejection.

顾名思义, Array.reject方法用于拒绝Array中的某些元素。 如果元素满足块内提供的条件或标准,则不会打印这些元素。 此方法是一种非破坏性方法,不会对Array对象的实际值带来任何变化。 该方法根据您将在一对括号内提供的某些条件来工作。 此方法完全基于您在块内提供的条件。 即使您未在块内指定任何条件,此方法也将起作用。 如果您未提供任何拒绝条件或条件,它将打印所有值。

Syntax:

句法:

    Array.reject{|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 == 'b'
puts "Odd numbers are:"
puts num.reject{|num|
num%2 == 0
}
elsif opt == 'a'
puts "Even numbers are:"
puts num.reject{|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.reject 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. It is rejecting the elements which are satisfying the condition provided inside the block.

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

Example 2:

范例2:

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

Output

输出量

2
44
2
5
7
83
5
67
12
11
90
78
9

Explanation:

说明:

In the above output, you can observe that when you are not specifying any condition inside the method, it is still not throwing any kind of exception inside it will print all the elements present in the Array instance. This is one more point by which we can differentiate Array.select and Array.reject.

在上面的输出中,您可以观察到,当您在方法内未指定任何条件时,它仍未引发任何异常,它将打印Array实例中存在的所有元素。 这是我们可以区分Array.select和Array.reject的另一点。

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

ruby中、.reject

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

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

相关文章

java获取主机mac_Java 如何获取主机的MAC地址

获取MAC地址首先要理解当前的操作系统,由于在不同的操作系统中CMD命令所在的位置不同,因此首先使用System类中的getProperty("os.name")方法获取当前的操作系统,getProperty()方法可以确定当前系统属性,它的参数是一些固…

微软免费软件项目DreamSpark更名为Microsoft Imagine

9月10日消息,微软免费软件项目DreamSpark近日正式更名为Microsoft Imagine,将与一年一度的微软“创新杯(Imagine Cup)”齐名。微软免费软件项目DreamSpark更名为Microsoft Imagine  2008年2月19日,微软公司董事长比尔盖茨在斯坦福大学发布了…

java jpa_Java JPA 语法知识

前提操作创建一个可持久化的实体类dao层继承JpaRepositoryT:实体类ID:实体类的主键类型例:public interface SysUserRespository extends JpaRepository {}JPA中支持的关键词And --- 等价于 SQL 中的 and 关键字,比如 findByUsern…

array.slice_Ruby中带有示例的Array.slice()方法

array.sliceArray.slice()方法 (Array.slice() Method) In this article, we will study about Array.slice() method. You all must be thinking the method must be doing something which is related to the slicing of elements or objects in the Array instance. It is n…

阿特斯携手EDF启动建设巴西191.5MW光伏项目

2016年10月11日,阿特斯太阳能(安大略省,圭尔夫)和EDF Energies Nouvelles(法国,巴黎)共同宣布,将阿特斯巴西Pirapora I太阳能项目80%的股权出售给EDF的巴西本地子公司EDF…

apachejmeter_java源码_自定义编写jmeter的Java测试代码

我们在做性能测试时,有时需要自己编写测试脚本,很多测试工具都支持自定义编写测试脚本,比如LoadRunner就有很多自定义脚本的协议,比如"C Vuser","JavaVuser"等协议.同样,Jmeter也支持自定义编写的测试代码,不过与LoadRunner不同的是,Jmeter没有自带编译器,…

julia fit 函数_带有Julia中示例的flipsign()函数

julia fit 函数Julia| flipsign()函数 (Julia | flipsign() function) flipsign() function is a library function in Julia programming language, it accepts two values as parameters and returns a value with the magnitude of first value and sign of the first value…

优化Android应用内存的若干方法

https://my.oschina.net/chaselinfo/blog/198172摘要: 在app开发的各个阶段中要考虑RAM的限制问题, 包括在设计阶段(正式开发之前). 使用下面的不同的方法可以达到很好的效果. 当您在设计和开发Android应用时用下面的方法可以使内存运用最高效.使用保守的Service 如果你的应用需…

一? ilkkn.n_IL&FS的完整形式是什么?

一? il&kkn.nIL&FS:基础设施租赁和金融服务 (IL& FS: Infrastructure Leasing & Financial Services) IL&FS is an abbreviation of Infrastructure Leasing & Financial Services. It is the largest infrastructure development …

java notify唤醒原理_Java wait和notify虚假唤醒原理

自己在此记录一下,方便日后复习。虚假唤醒的概念jdk官方文档解释:所以说在wait和notify一块使用时,如果使用if作为条件时,会有虚假唤醒的情况发生,所以必须使用while作为循环条件。下面来举例实验:首先&…

C#里面的三种定时计时器:Timer

在.NET中有三种计时器:1、System.Windows.Forms命名空间下的Timer控件,它直接继承自Componet。Timer控件只有绑定了Tick事件和设置EnabledTrue后才会自动计时,停止计时可以用Stop()方法控制,通过Stop()停止之后,如果想…

wireshark rto_RTO的完整形式是什么?

wireshark rtoRTO:地区运输办公室/公路运输办公室 (RTO: Regional Transport Office/ Road Transport Office) RTO is an abbreviation of the Regional Transport Office. It is an Indian Government departmental organization that is responsible for upholdin…

java8 json转xml_2019-08-17java对象与json,xml互转

依赖的jar包,jackson-all-1.7.6.jar,xstream-1.4.4.jar下载地址:链接:https://pan.baidu.com/s/1LflD135qlQiIPGXw5XwDmw提取码:6v29复制这段内容后打开百度网盘手机App,操作更方便哦package json_xml;import com.thoughtworks.xs…

10.8-全栈Java笔记:序列化/反序列化的步骤和实例

本节我们详细讲解10.3节中提到的序列化和反序列化操作。序列化和反序列化是什么当两个进程远程通信时,彼此可以发送各种类型的数据。 无论是何种类型的数据,都会以二进制序列的形式在网络上传送。比如,我们可以通过http协议发送字符串信息&am…

有效的网络推广超级实用方法

我叫龙雨,先后在百度搜狗工作过3年,后来一直负责一家公司的的网络营销!不知道大家有没有听过111>3这样一个概念,简单来说一下这概念!第一呢就是自己的资源,把自己的资源维护好开发好;第二就是网络营销,网络营销利用…

什么为java运行时的环境_什么是JRE?Java运行时环境简介(一)

Java开发工具包(JDK),Java虚拟机(JVM)和Java运行时环境(JRE)共同构成了用于开发和运行Java应用程序的Java平台组件的强大功能.实际上,运行时环境是一种旨在运行其他软件的软件.作为Java的运行时环境,JRE包含Java类库,Java类加载器和Java虚拟机.在这个系统中:的类加载器是负责正…

c语言atoll函数怎么用_C ++中带有示例的atoll()函数

c语言atoll函数怎么用C Atoll()函数 (C atoll() function) atoll() function is a library function of cstdlib header. It is used to convert the given string value to the integer value. It accepts a string containing an integer (integral) number and returns its…

看清美国“黑客帝国”的真面目

“维基揭秘”网站近日发布了近9000份据称属于美国中央情报局的机密文件,显示中情局拥有强大的黑客攻击能力,秘密侵入了手机、电脑、智能电视等众多智能设备。继美国国家安全局承包商前雇员斯诺登曝光国安局“棱镜”等监控计划之后,此次曝光再…

python 示例_带有示例的Python File close()方法

python 示例文件close()方法 (File close() Method) close() method is an inbuilt method in Python, it is used to flush and close the IO object (file). If we close a closed file it has no effect. close()方法是Python中的内置方法,用于刷新和关闭IO对象(…

linux上mysql分区磁盘位置_Linux下Oracle软件、数据文件等所在的磁盘分区空间不足的解决思路...

虚拟机中的ORACLE运行的久了,归档、数据文件不断增长,原来安装ORACLE的分区空间不足。此时可以重新向虚拟机增加一块硬盘,将OR虚拟机中的Oracle运行的久了,归档、数据文件不断增长,原来安装ORACLE的分区空间不足。此时…