Ruby program to create a SortedSet of integer elements

Ruby Example: Write a program to create a SortedSet of integer elements.
Submitted by Nidhi, on February 07, 2022

Problem Solution:

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

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.

Ruby Set Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.