ruby end if_Ruby Set方法| delete_if()和keep_if()

ruby end if

There are several methods present in the Ruby library specifically defined to fulfill the requirements of Set objects. delete_if() and keep_if() are two functions which have certain specified tasks. The purpose of both the methods is the opposite of each other but they have a similarity that the executions of both are dependent upon a certain Boolean condition. If the Boolean condition comes out to be true then only the method will be executed, otherwise, they would not be executed. Let us see what they both are being used for. The rest of the article will let you know about the implementation of both methods with the help of examples.

Ruby库中存在几种专门定义的方法,这些方法可以满足Set对象的要求。 delete_if()和keep_if()是具有某些指定任务的两个函数。 这两种方法的目的是相反的,但它们的相似之处在于两者的执行都取决于特定的布尔条件。 如果布尔条件成立,则仅将执行该方法,否则将不执行它们。 让我们看看它们都被用来做什么。 本文的其余部分将通过示例帮助您了解这两种方法的实现。

delete_if()方法 (delete_if() method)

delete_if() method is used to delete all the elements of the Set object or you can say that it can delete the whole set at once but it is dependent upon a condition. If that 'if' condition comes out to be true which is sheltering delete_if() method, then all the contents of the Set will be deleted otherwise no element will be deleted from the instance of Set class.

delete_if()方法用于删除Set对象的所有元素,或者可以说它可以一次删除整个集合,但取决于条件。 如果隐含delete_if()方法的 'if'条件成立,则Set中的所有内容都将被删除,否则不会从Set类的实例中删除任何元素。

This method does not take any argument. It returns self moreover, it returns enumerator if you will not provide it a block. Let us see its example for having a better understanding of its concept.

此方法不带任何参数。 此外,它返回self,如果不为它提供块,则返回枚举器。 让我们看看它的示例,以更好地理解其概念。

Example:

例:

=begin
Ruby program to implement delete_if?().
=end
require 'set'

Vegetable=Set.new(["potato","brocolli","broccoflower","lentils","peas","fennel","chilli","cabbage"])

puts "Enter the number of element you want in the Set:"
num = gets.chomp.to_i

if Vegetable.count > num
	Vegetable.delete_if()
	puts "Set overloaded. Set emptied"
else
	puts "Set is not overloaded"
end

Output

输出量

RUN 1:
Enter the number of element you want in the Set:
 12
Set is not overloaded

RUN 2:
Enter the number of element you want in the Set:
 2
Set overloaded. Set emptied

Explanation:

说明:

In the above code, you can observe that we are making use of the Set.delete_if() method. This method is deleting all the elements from the Set if the set is having more elements than the requirement of the user. The condition is put inside an if condition; if that condition comes out to be true then the whole set will be deleted otherwise there will be no effect on the set.

在上面的代码中,您可以观察到我们正在使用Set.delete_if()方法 。 如果集合中的元素数超过了用户的要求,则此方法将从集合中删除所有元素。 该条件放在if条件内; 如果该条件成立,则整个集合将被删除,否则对集合没有影响。

keep_if()方法 (keep_if() method)

This method is just the opposite of the Set.delete_if() method. This method will keep the set if the condition comes out to be true or you can say that it will delete all the elements of the Set if the condition comes out to be false.

该方法与Set.delete_if()方法相反。 如果条件为真,则此方法将保留集合,或者可以说如果条件为假,则它将删除Set的所有元素。

This method does not take any arguments. If will return self and if no block is specified, it will return an enumerator.

此方法不带任何参数。 如果将返回self并且如果未指定任何块,则它将返回一个枚举数。

Example:

例:

=begin
Ruby program to implement keep_if?().
=end

require 'set'

Vegetable=Set.new(["potato","brocolli","broccoflower","lentils","peas","fennel","chilli","cabbage"])

puts "Enter the number of element you want in the Set:"
num = gets.chomp.to_i

if Vegetable.count < num
	Vegetable.keep_if()
	puts "Set is not overloaded"
else
	puts "Set is overloaded"
end

Output

输出量

RUN 1:
Enter the number of element you want in the Set:
 12
Set is not overloaded

RUN 2:
Enter the number of element you want in the Set:
 2
Set is overloaded

Example:

例:

In the above code, you can observe that we are making use of the Set.keep_if() method. This method is deleting all the elements from the Set if the set is having more elements than the requirement of the user. The condition is put inside if condition; if that condition comes out to be false then the whole set will be deleted otherwise there will be no effect on the set.

在上面的代码中,您可以观察到我们正在使用Set.keep_if()方法 。 如果集合中的元素数超过了用户的要求,则此方法将从集合中删除所有元素。 条件被放入条件中; 如果该条件为假,则整个集合将被删除,否则对集合没有影响。

翻译自: https://www.includehelp.com/ruby/set-methods-delete_if-and-keep_if.aspx

ruby end if

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值