ADVERTISEMENT
ADVERTISEMENT

Ruby program to create a set of integer elements

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

Problem Solution:

In this program, we will create the object of the Set class. Then we will add the elements into the set using the "<<" operator and print the created set.

Program/Source Code:

The source code to create a set of integer elements is given below. The given program is compiled and executed successfully.

# Ruby program to create a set 
# of integer elements

require 'set';

setObj = Set.new();

setObj << 101;
setObj << 102;
setObj << 103;

puts "Set elements: ",setObj;

Output:

Set elements: 
#<Set: {101, 102, 103}>

Explanation:

In the above program, we imported the "set" package using the "require" statement. Then we created the object setObj of the Set class using the new() method. After that, we added 3 integer items into the created set and printed the created set.

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.