Open In App

Ruby | Set merge() function

Last Updated : 07 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The merge() is an inbuilt method in Ruby returns the new set after merging the passed objects into a set.

Syntax: s1_name.merge(object)

Parameters: The function accepts a mandatory parameter enumerable object which is to be merged for.

Return Value: It returns the new set after merging.

Example 1:




# Ruby program to illustrate 
# the merge method 
   
# requires the set 
require "set"
   
s1 = Set[1, 2, 3]
s2 = Set[6]
   
# merge method used
puts s1.merge(s2)


Output:

Set: {1, 2, 3, 6}

Example 2:




# Ruby program to illustrate 
# the merge method 
   
# requires the set 
require "set"
   
s1 = Set[1, 2, 3]
   
# merge method used
puts s1.merge([9, 10, 12])


Output:

Set: {1, 2, 3, 9, 10, 12}

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads