Q:

Ruby program to create a SortedSet of integer elements

belongs to collection: Ruby Set Programs

0

In this program, we will create a sorted sort with integer elements using SortedSet class. Then we will print set elements in sorted order.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Ruby program to check a set is a subset of another...