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方法相关推荐

  1. 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 ...

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

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

  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.cycle()方法

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

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

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

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

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

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

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

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

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

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

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

最新文章

  1. Python将两个图像合并成一个图像(横向合并)
  2. createjs开发h5游戏: 指尖大冒险
  3. 快速书写常见的 Kotlin 代码 MD
  4. OpenGL-渲染管线的流程(有图有真相)
  5. JavaScript模板引擎原理,几行代码的事儿
  6. dw_mysql】apache_怎么将dreamweaver与apache服务器连接
  7. jmeter ForEach控制器学习
  8. android ndk gcc,Android NDK GCC似乎是铿锵的 - 它应该是这样吗?
  9. Kafka 2.8与ZooKeeper正式分手
  10. python语言基础笔记_Python语言 基础知识笔记
  11. android google snake
  12. 首个 Ubuntu 平板将于今年秋天面世
  13. 什么是Liunx?什么是Shell脚本?Liunx的应用、Shell脚本编写与应用
  14. .NET定位CPU使用率过高问题
  15. 物联网的云计算、雾计算、边缘计算和MIST计算的基本概念
  16. Flutter 功能最全的JsonToDart工具
  17. 榆熙教育有限公司:拼多多商家应该了解的常用推广方式
  18. 10.配置Elasticsearch
  19. DHT11温湿度传感器学习笔记
  20. 如何查看自己steam库里游戏是哪个区的

热门文章

  1. MySQL学习(4)多表查询
  2. 在Ubuntu 8.04 LTS(hardy)下安装配置nginx和fastcgi方式的php
  3. spring cloud eureka服务注册和调用
  4. VR究竟多奇幻?eSmart邀你共赴一场VR奇幻之旅!
  5. hadoop-eclipse-plugin使用
  6. SCOM 2012知识分享-9:配置警报解决状态
  7. jQuery.grep()
  8. 【杨中科】问苍天,微软的技术更新真的快吗
  9. 管道过滤器(Pipe-And-Filter)模式
  10. 51CTO寄来的奖品