ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.