ruby array_Ruby中带有示例的Array.delete_if方法
ruby array
Ruby Array.delete_if方法 (Ruby Array.delete_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 method destructive, you can add an "!" after the method name. For instance, Array.select! is the destructive version of Array.select.
在上一篇文章中,我们研究了Array方法,即Array.select , Array.reject和Array.drop_While ,所有这些方法都是非破坏性的方法,这意味着它们不对驻留在数组中的元素的实际值施加任何更改。数组实例。 如果要使上述方法具有破坏性,则可以添加“!” 方法名称之后。 例如, Array.select! 是Array.select的破坏性版本。
In this article, we will learn about the Array method Array.delete_if which is already destructive by nature.
在本文中,我们将学习Array方法Array.delete_if ,该方法本质上已经具有破坏性。
Method description:
方法说明:
The changes created by this method are always permanent as it is one of a kind of destructive methods. This method works in a way that if it finds an element which is not satisfying the Boolean condition which is specified inside the block of the method, then it deletes that element from the Array instance. It will not delete any element if it does not find the Boolean condition specified inside the block of the method.
通过这种方法创建的更改始终是永久性的,因为它是一种破坏性方法。 此方法的工作方式是,如果找到不满足该方法块内指定的布尔条件的元素,则将其从Array实例中删除。 如果找不到方法块内指定的布尔条件,它将不会删除任何元素。
Syntax:
句法:
Array.delete_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.delete_if
=end
# array declaration
num = [1,2,3,4,5,6,7,8,9,10,23,11,33,55,66,12]
# input
puts "Enter the your choice (a)delete odd numbers (b) delete even numbers"
lm = gets.chomp
if lm == 'a'
puts "Even numbers are:"
puts num.delete_if { |a| a % 2 !=0 }
elsif lm == 'b'
puts "Odd numbers are:"
puts num.delete_if { |a| a % 2 ==0 }
else
puts "Invalid Input"
end
Output
输出量
RUN 1:
Enter the your choice (a)delete odd numbers (b) delete even numbers
a
Even numbers are:
2
4
6
8
10
66
12
RUN 2:
Enter the your choice (a)delete odd numbers (b) delete even 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 deleting all the elements which are satisfying the condition provided inside the block of Array.delete_if method. If the user is asking to delete odd numbers, then output is shown in RUN2 and when the user is asking for even numbers, the output is shown in the RUN1.
在上面的代码中,您可以观察到该方法正在删除所有满足Array.delete_if method块中提供的条件的元素。 如果用户要求删除奇数,则在RUN2中显示输出,而当用户要求偶数时,在RUN1中显示输出。
Example 2:
范例2:
=begin
Ruby program to demonstrate Array.delete_if
=end
# array declaration
num = [1,2,3,4,5,6,7,8,9,10,23,11,33,55,66,12]
print num.delete_if{|a|}
puts ""
print num
Output
输出量
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 23, 11, 33, 55, 66, 12]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 23, 11, 33, 55, 66, 12]
Explanation:
说明:
In the above code, you can observe that if you are not specifying any condition inside the method block, then it is not deleting or removing any element from the Array instance.
在上面的代码中,您可以观察到,如果您未在方法块内指定任何条件,那么它就不会从Array实例中删除或删除任何元素。
翻译自: https://www.includehelp.com/ruby/array-delete_if-method-with-example.aspx
ruby array
ruby array_Ruby中带有示例的Array.delete_if方法相关推荐
- 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 ...
- ruby array_Ruby中带有示例的Array.shuffle方法
ruby array Array.shuffle方法 (Array.shuffle Method) In this article, we will study about Array.shuffle ...
- ruby array_Ruby中带有示例的Array.select方法
ruby array Array.select方法 (Array.select Method) In the last articles, we have seen how to iterate ov ...
- ruby array_Ruby中带有示例的Array.cycle()方法
ruby array Array.cycle()方法 (Array.cycle() Method) In this article, we will study about Array.cycle() ...
- ruby array_Ruby中带有示例的Array.fill()方法(1)
ruby array Array.fill()方法 (Array.fill() Method) In this article, we will study about Array.fill() me ...
- ruby array_Ruby中带有示例的Array.index()方法
ruby array Array.index()方法 (Array.index() Method) In this article, we will study about Array.index() ...
- ruby array_Ruby中带有示例的Array.sample()方法
ruby array Array.sample()方法 (Array.sample() Method) In this article, we will study about Array.sampl ...
- ruby array_Ruby中带有示例的Array.fill()方法(3)
ruby array Array.fill()方法 (Array.fill() Method) In this article, we will study about Array.fill() me ...
- ruby array_Ruby中带有示例的Array.zip()方法
ruby array Array.zip()方法 (Array.zip() Method) In this article, we will study about Array.zip() Metho ...
最新文章
- Python将两个图像合并成一个图像(横向合并)
- createjs开发h5游戏: 指尖大冒险
- 快速书写常见的 Kotlin 代码 MD
- OpenGL-渲染管线的流程(有图有真相)
- JavaScript模板引擎原理,几行代码的事儿
- dw_mysql】apache_怎么将dreamweaver与apache服务器连接
- jmeter ForEach控制器学习
- android ndk gcc,Android NDK GCC似乎是铿锵的 - 它应该是这样吗?
- Kafka 2.8与ZooKeeper正式分手
- python语言基础笔记_Python语言 基础知识笔记
- android google snake
- 首个 Ubuntu 平板将于今年秋天面世
- 什么是Liunx?什么是Shell脚本?Liunx的应用、Shell脚本编写与应用
- .NET定位CPU使用率过高问题
- 物联网的云计算、雾计算、边缘计算和MIST计算的基本概念
- Flutter 功能最全的JsonToDart工具
- 榆熙教育有限公司:拼多多商家应该了解的常用推广方式
- 10.配置Elasticsearch
- DHT11温湿度传感器学习笔记
- 如何查看自己steam库里游戏是哪个区的