Ruby | Set subtract() function

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Set: {12, 78, 98}

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Set: {4, 6, 10}
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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.