Q:

Ruby program to create a set of integer elements

0

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.

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 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.

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now