Home » Ruby programming

Ruby Set methods | delete_if() and keep_if()

Ruby Set methods | delete_if() and keep_if(): Here, we are going to learn about the delete_if() and keep_if() methods in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on November 15, 2019

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.

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.

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.

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.

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.

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

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.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.