The delete()
method is used to delete an element in a set. To delete a couple of elements that meet a particular condition, we must use the delete_if()
condition. If the block of an element returns true, the element is deleted. The block here takes each element as it loops the set, and if the element satisfies its condition, it is removed.
set.delete_if(condition)
condition
: This is the block condition that is specified for each element.
It returns a new set with some of its elements deleted.
# require the set classrequire "set"# create a setRandomNumbers = Set.new([1, 4, 29, 12, 30, 66, 72])puts "Previous Elements:"puts RandomNumbers.to_a# delete elements that are even NumbersRandomNumbers.delete_if{|num| num % 2 == 0}puts "\nElements remaining:"puts RandomNumbers.to_a
require
to obtain a set
class.RandomNumbers
. to_a
method to print the set to the console as an array.