Skip to main content

Ruby Set

In ruby programming the set is collection of unique objects. Set element is unordered sequence which is not maintain order. It is easier to perform operation in two sets, such as equality, union, intersection etc. In a Ruby program, sets are used by including the statement (required "set"). After this we can take advantage of the working efficiency of the set.

require "set"
# Empty sets
record1 =
record2 = Set[]
# Display type
print(" Type record1 : ",record1.class)
# Display size
print("\n Size record2 : ",record2.size)
 Type record1 : Set
 Size record2 : 0

How to create set in ruby

In Ruby programming, there are two ways to create a set, the first and the second set[]. In given below mention few example.

require "set"
# empty set
a =

# Set with initial value
b = Set[1,2,3]

# Set with array element
c =[4,5,6,7])

# Define an array
arr = [10,2020]
# typecast to create set
d = arr.to_set 

# Create set using range
e =

# Create set using range with condition
f = {|value| value * 2}

# Display size
print(" Set a size is ",a.size())
print("\n Set b size is ",b.size())
print("\n Set c size is ",c.size())
print("\n Set d size is ",d.size())
print("\n Set e size is ",e.size())
print("\n Set f size is ",f.size())
Create Ruby Set
 Set a size is 0
 Set b size is 3
 Set c size is 4
 Set d size is 2
 Set e size is 5
 Set f size is 3

Set are contain object elements, So one set can be contain more than one objects or empty value.

require "set"
# Create set of different objects
record =[1,"Two",nil,[10,20,30],89.3,'A'])
# Show number of objects
Create Ruby set with different objects

Ruby Set Method

There are following methods are available in set, which are control the set operation.

Method Overview
Set[] Creating and returning new set using given objects. Creates a new set containing the elements of the given objects.
add(obj) Adds the given object to the set and return self.
difference() Returns a new set built by duplicating the set, removing every element that appears in the given enumerable object.
replace(enum) Modify a given set by enumerable objects.
union(enum) This method are returns new set which contains unique elements of apply set and given objects.
flatten() This method are returns a new set that is a copy of the set.
intersect?(otherset) This method returning true value if the set and the given set have at least one element in common.
divide() This method divides the set into a set of subsets according to the commonality defined by the given block.
delete() Deletes the given object from the set and return self.
intersection(enum) Returns a new set containing elements common in apply and enumerable object.
to_a() returns an array which contains set element.
set.subtract(enumerable) This method deletes every element that appears in the given enumerable object and returns self.
subset?(set) This method returns true if the set is a subset of the given set.

size() method are used to get the number of element in set. But set are not allow direct access by index so we can use its by using convert into an array.

How to iterating the elements of set

Generally Set elements are not access by index. We can iterate the elements using each statement.

require "set"
# Create set
record =[1,6,3,8,10])

record.each {
    # Display object value  
    puts x  

In other way convert set into an array and access its elements. After changing the set in the array, its elements can be easily accessed and printed. See this example.

require "set"
# Create set 
record =[1,6,3,8,10])
arr = record.to_a
# Display array
# Second way
for i in arr
[1, 6, 3, 8, 10]


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment