Skip to content
Related Articles

Related Articles

Ruby | Set subtract() function
  • Last Updated : 07 Jan, 2020

The subtract() is an inbuilt method in Ruby returns the set after deleting all the objects that appear in the enum that is passed.

Syntax: s1_name.subtract(enum)

Parameters: The function takes an object enum whose elements are deleted from the set.

Return Value: It returns self object after removing all elements are removed of enum from set.

Example 1:






# Ruby program to illustrate 
# the subtract() method 
   
# requires the set 
require "set"
   
s1 = Set[2, 12, 78, 10, 87, 98
s2 = Set[2, 10, 87]
   
# subtract method used 
puts s1.subtract(s2)

Output:

Set: {12, 78, 98}

Example 2:




# Ruby program to illustrate 
# the subtract() method 
   
# requires the set 
require "set"
   
s1 = Set[4, 5, 6, 7, 10
   
# subtract method used 
puts s1.subtract([5, 7])

Output:

Set: {4, 6, 10}

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 :