ADVERTISEMENT
ADVERTISEMENT

Ruby program to convert a set into an array

Ruby Example: Write a program to convert a set into an array.
Submitted by Nidhi, on February 06, 2022

Problem Solution:

In this program, we will create the object of the Set class and add items to the set. Then we will convert created set into the array using the to_a() method.

Program/Source Code:

The source code to convert a set into the array is given below. The given program is compiled and executed successfully.

# Ruby program to convert a set 
# into an array

require 'set';

setObj = Set.new();

setObj.add(101);
setObj.add(102);
setObj.add(103);
setObj.add(104);
setObj.add(105);

arr = setObj.to_a();

puts "Set is: ",setObj;
puts "Array is: ",arr;

Output:

Set is: 
#<Set: {101, 102, 103, 104, 105}>
Array is: 
101
102
103
104
105

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 and added 5 integer items into created set using add() method of the Set class. After that, we converted the created set into an array and assigned it to the array arr, and we printed the result.

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.