Removing or deleting an element from a Set in Ruby is easy to do by using the delete()
method. This method takes the element we want to remove as a parameter.
set.delete(element)
element: This is the element we want to delete from the set.
A new array is returned with element
removed.
# require the set Class to use itrequire "set"# create a setLanguages = Set.new(["Ruby", "Java", "PHP", "JavaScript", "Python"])# print previous elementsputs "PREVIOUS ELEMENTS:\n"for element in Languages doputs elementend# delete some elements from the setLanguages.delete("JavaScript")Languages.delete("Java")Languages.delete("Python")# print current elementsputs "\nCURRENT ELEMENTS:"for element in Languages doputs elementend
for
loop, we print the elements in the set we just created.delete()
method of the set instance, we deleted some elements to the set Languages
that we created.for
loop, we print out the elements of the modified set.