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

ruby中、.reject_Ruby中带有示例的Array.reject方法相关推荐

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

    ruby array Array.shuffle方法 (Array.shuffle Method) In this article, we will study about Array.shuffle ...

  2. ruby .each_Ruby中带有示例的Array.each方法

    ruby .each Ruby Array.each方法 (Ruby Array.each method) Array.each method can be easily termed as a me ...

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

    ruby array Array.select方法 (Array.select Method) In the last articles, we have seen how to iterate ov ...

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

    ruby array Ruby Array.keep_if方法 (Ruby Array.keep_if Method) In the last articles, we have studied th ...

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

    ruby array Ruby Array.delete_if方法 (Ruby Array.delete_if Method) In the last articles, we have studie ...

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

    ruby array Array.cycle()方法 (Array.cycle() Method) In this article, we will study about Array.cycle() ...

  7. ruby array_Ruby中带有示例的Array.fill()方法(1)

    ruby array Array.fill()方法 (Array.fill() Method) In this article, we will study about Array.fill() me ...

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

    ruby array Array.index()方法 (Array.index() Method) In this article, we will study about Array.index() ...

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

    ruby array Array.sample()方法 (Array.sample() Method) In this article, we will study about Array.sampl ...

最新文章

  1. openstack对比其他有什么优点
  2. 通知 | 2020年度“RONG”奖学金入围答辩名单公布
  3. 求职该选好公司还是好上司
  4. Android之自定义标题
  5. Kafka分区分配策略(4)——分配的实施
  6. Spring Data JPA教程:获取所需的依赖关系
  7. OSPF单域实验报告
  8. 名爵如何解除限速_价格不高却乐趣十足,全新一代名爵6有多大惊喜
  9. html5 下拉框 美化,纯js超酷select下拉框美化插件
  10. python输入一个假分数_腾讯内容开放平台
  11. java 包装类缺点_Java 自动拆箱和自动装箱学习笔记
  12. 第二章 HTML5存储 Web存储
  13. java每轮排序结果,冒泡排序及其优化java
  14. 淘客外卖返利小程序搭建 - 美团联盟外卖返利小程序平台、饿了么返利小程序系统、外卖cps小程序
  15. WingIDE5注册破解的方法
  16. 智慧树python第三章答案_智慧树Python语言应用第三单元章节测试答案选修课网课慕课答案...
  17. 不管你学的是什么专业,你都应该多少懂些 (来自qq空间)(分享)(转载)
  18. 【BZOJ4327】[JSOI2012] 玄武密码(AC自动机的小应用)
  19. soap-ws 获取wsdl中所有方法 (一)
  20. 微信小程序开发之.js文件

热门文章

  1. python init方法做了什么_Python类方法、__new__方法和__init__方法分别是什么
  2. window.open不重复打开同一个名称的窗口_干货满满|Ctrl键的正确打开方式
  3. mysql增加实例,MySQL的简单使用笔记:增加实例以及启动
  4. 都2021年了,还不会使用GitHub创建、推送、拉取、克隆远程库、团队协作开发?
  5. 知识蒸馏 knowledge distill 相关论文理解
  6. 5-Dockerfile文件
  7. 猫哥教你写爬虫 005--数据类型转换-小作业
  8. gulp之gulp.watch报错
  9. 窄带物联网(NB-IoT)初步了解
  10. tomcat 多项目多HOST配置