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.selectArray.rejectArray.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值