The replace()
method of a set is used to replace the set's contents. The contents of the set are replaced with new content that we specify. The content that will replace the previous one is an enumerable object, for example, an array.
set.replace(object)
object
: This is the object containing the elements that will replace the elements or contents of the set
.
# require the set classrequire "set"# create some setsEvenNumbers = Set.new([2, 4, 6, 8, 10])NumbersToTen = Set.new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])Developers = Set.new(["Amaka", "Chioma", "Titi"])# check if proper supersets existsEvenNumbers.replace([24])NumbersToTen.replace([30, 22, 4, 1])Developers.replace(["Peter", "Jane"])# print setsputs "#{EvenNumbers.to_a}"puts "#{NumbersToTen.to_a}"puts "#{Developers.to_a}"
require
the set class for all set operations.replace()
method, we replace the contents of the set with some new values.