Open In App

Ruby | Set subtract() function

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
1 Likes
Like
Report
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
# 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
# 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}

Explore