The source code to create a SortedSet of integer elements is given below. The given program is compiled and executed successfully.
# Ruby program to create a SortedSet
# of integer elements
require 'set';
setObj = SortedSet.new();
setObj << 101;
setObj << 103;
setObj << 104;
setObj << 102;
puts "SortedSet elements: ";
for item in setObj
print item," ";
end
Output:
SortedSet elements:
101 102 103 104
Explanation:
In the above program, we imported the "set" package using the "require" statement. Then we created an object setObj of SortedSet class using the new() method and added items into created set using add() method of SortedSet class. After that, we printed sorted items.
Program/Source Code:
The source code to create a SortedSet of integer elements is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we imported the "set" package using the "require" statement. Then we created an object setObj of SortedSet class using the new() method and added items into created set using add() method of SortedSet class. After that, we printed sorted items.
need an explanation for this answer? contact us directly to get an explanation for this answer