In this program, we will create the object of the Set class. Then we will add the elements into the set using add() method of the Set class and print the created set.
The source code to create add an element into the set using add() method is given below. The given program is compiled and executed successfully.
# Ruby program to create add an element into set
# using add() method
require 'set';
setObj = Set.new();
setObj.add(101);
setObj.add(102);
setObj.add(103);
setObj.add(104);
setObj.add(105);
puts "Set elements: ",setObj;
Output:
Set elements:
#<Set: {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. After that, we added 5 integer items into created set using add() method of the Set class and printed the created set.
Program/Source Code:
The source code to create add an element into the set using add() method is given below. The given program is compiled and executed successfully.
Output:
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 5 integer items into created set using add() method of the Set class and printed the created set.
need an explanation for this answer? contact us directly to get an explanation for this answer