Ruby | Set delete() function

The delete() is an inbuilt method in Ruby which deletes the given object from the set and returns the self object. In case the object is not present, it returns self only.

Syntax: s1.name.delete(object)

Parameters: The function takes a mandatory parameter object which is to be deleted.



Return Value: It returns self after deletion of the object from the set.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#Ruby program to illustrate the
#delete method
  
#requires the set
require "set"
  
    s1
    = Set[1, 2, 3]
  
#deletes 2 and prints self
      puts s1.delete(2)
  
#deletes 1 and prints self
          puts s1.delete(1)
  
#deletes 4 and prints self
              puts s1.delete(4)

chevron_right


Output:

Set: {1, 3}
Set: {3}
Set: {3}

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#Ruby program to illustrate the
#delete method
  
#requires the set
require "set"
  
    s1
    = Set[11, 12, 33, "a"]
  
#deletes 'a' and prints self
      puts s1.delete("a")
  
#deletes 11 and prints self
          puts s1.delete(11)
  
#deletes 33 and prints self
              puts s1.delete(33)

chevron_right


Output:

Set: {11, 12, 33}
Set: {12, 33}
Set: {12}

Reference: https://devdocs.io/ruby~2.5/set#method-i-delete



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.