Ruby | Set replace() function Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The replace() is an inbuilt method in Ruby which replaces the contents of the set with the contents of the given enumerable object and returns self. Syntax: s1.replace(enum) Parameters: The function accepts an enumerable object which to be replaced in the set. Return Value: It returns the self object which contains the contents of the set after replacement. Example 1: Ruby # Ruby program to illustrate # the replace() method # requires the set require "set" s1 = Set[1, 2] s2 = Set[1, 2, 3] # replace method used puts s1.replace(s2) # s1 after replacement puts s1 Output: Set: {1, 2, 3} Set: {1, 2, 3} Example 2: Ruby # Ruby program to illustrate # the replace() method # requires the set require "set" s1 = Set[4, 4] s2 = Set[2, 12, 78, 87, 98] # replace method used puts s2.replace(s1) # s1 after replacement puts s2 Output: Set: {4} Set: {4} Comment More infoAdvertise with us Next Article Ruby | Regexp to_s() function G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Set-class Similar Reads Ruby | Array replace() function Array#replace() : replace() is a Array class method which returns an array of all combinations of elements from all arrays. Syntax: Array.replace() Parameter: Array Return: an array of all combinations of elements from all arrays. Example #1 : Ruby # Ruby code for replace() method # declaring array 2 min read Ruby | Regexp to_s() function Regexp#to_s() : to_s() is a Regexp class method which returns the string containing the regular expression with the same semantics. Syntax: Regexp.to_s() Parameter: Regexp values Return: string containing the regular expression with the same semantics. Example #1 : Ruby # Ruby code for Regexp.to_s() 1 min read Ruby | Regexp source() function Regexp#source() : source() is a Regexp class method which returns the original string of the pattern. Syntax: Regexp.source() Parameter: Regexp values Return: original string of the pattern. Example #1 : Ruby # Ruby code for Regexp.source() method # declaring Regexp value reg_a = /a/ # declaring Reg 1 min read Ruby | Regexp =~() function Regexp#=~() : =~() is a Regexp class method which matches a regular expression against a string. Syntax: Regexp.=~()Parameter: Regexp valuesReturn: true - if two regular expressions matches string otherwise return false Example #1 :  Ruby # Ruby code for Regexp.=~() method # declaring Regexp value 1 min read Ruby | Regexp ==() function Regexp#==() : ==() is a Regexp class method which compares two regular expressions. Syntax: Regexp.==() Parameter: Regexp values Return: true - if two regular expressions are equal otherwise return false Example #1 : Ruby # Ruby code for Regexp.==() method # declaring Regexp value reg_a = /a/ # decl 1 min read Ruby | Regexp ===() function Regexp#===() : ===() is a Regexp class method which compares the equality of two regular expressions. Syntax: Regexp.===() Parameter: Regexp values Return: true - if two regular expressions has equality otherwise return false Example #1 : Ruby # Ruby code for Regexp.===() method # declaring Regexp v 1 min read Like