Skip to content
Related Articles

Related Articles

Ruby | Set delete? function
  • Last Updated : 07 Jan, 2020
GeeksforGeeks - Summer Carnival Banner

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 nil.

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, and it returns nil if the object is not present.

Example 1:






#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)

Output:

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

Example 2:




#Ruby program to illustrate the
#delete ? method
  
#requires the set
require "set"
  
    s1
    = Set[4, 7, 13, "q"]
  
#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
    ? (13)

Output:

Set: {4, 7, "q"}

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :