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
推荐阅读
-
ruby .each_Ruby中带有示例的Array.each_index方法
-
ruby中、.reject_Ruby中带有示例的Array.reject方法
-
ruby array_Ruby中带有示例的Array.rotate()方法
-
iOS中设置圆角的几种方法示例
-
spring boot中内嵌redis的使用方法示例
-
PHP模板引擎Smarty之配置文件在模板变量中的使用方法示例
-
CMS中PHP判断系统是否已经安装的方法示例_PHP教程
-
Html5 APP中监听返回事件处理的方法示例
-
php去掉URL网址中带有PHPSESSID的配置方法
-
Python实现字典排序、按照list中字典的某个key排序的方法示例