Home »
Ruby Tutorial
Ruby Sets
By IncludeHelp Last updated : December 01, 2024
Set in Ruby
In Ruby, the set is a class that is very similar to an array but it has some special attributes which make the processing faster. You must have observed that you can insert duplicate elements in an array but this is not the case of the set. The set does not allow the insertion of non-unique elements implicitly. So, in the future, if you will add elements after the definition of a set, you need not worry about the duplicity. It would not allow the accommodation of repeated elements.
Declaring a Ruby Set
Following is the way to declare and define the Ruby set:
set_name = Set.new([element1,element2,...])
Now, let us see how we declare, define and implement a set with the help of some supporting examples?
Demonstrating the Use of Set
This example illustrates how to create and use a set in Ruby to store unique elements:
=begin
Ruby program to demonstrate set
=end
require 'set'
# Creation of new Set.
Vegetable = Set.new(["potato", "tomato","brinjal","onion"])
puts "Number of elements in set are #{Vegetable.size()}"
puts Vegetable
Output
Number of elements in set are 4
#<Set: {"potato", "tomato", "brinjal", "onion"}>
Explanation
In the above example, you can observe that we are creating a set named Vegetable as per the syntax given above. You can also observe that we need to put the statement require 'set' before starting to write code. We are only calculating the number of elements present in the set with the help of the set.size() method. Writing the set name along with the puts statement simply gives us the information about the set elements.
We have seen how to add elements in the set at the time of the declaration? Now, let us see how to add elements afterward?
Working with Sets
This example demonstrates creating a set, adding elements, and checking its size in Ruby:
=begin
Ruby program to demonstrate set
=end
require 'set'
# Creation of new Set.
Vegetable = Set.new(["potato", "tomato","brinjal","onion"])
puts "Number of elements in set are #{Vegetable.size()}"
puts Vegetable
Vegetable.add("Cauliflower")
Vegetable.add("Peas")
Vegetable.add("Beetroot")
puts "Number of elements in set are #{Vegetable.size()}"
puts Vegetable
Output
Number of elements in set are 4
#<Set: {"potato", "tomato", "brinjal", "onion"}>
Number of elements in set are 7
#<Set: {"potato", "tomato", "brinjal", "onion", "Cauliflower", "Peas", "Beetroot"}>
Explanation
In the code given above, you can observe that we are adding elements in the set Vegetable afterward with the help of set.add("Element") method. Then we are again printing the set size and the elements.
Set Operations
This example demonstrates how sets in Ruby handle unique elements, preventing duplicates when adding items:
=begin
Ruby program to demonstrate set
=end
require 'set'
# Creation of new Set.
Vegetable = Set.new(["potato", "tomato","brinjal","onion"])
puts "Number of elements in set are #{Vegetable.size()}"
puts Vegetable
Vegetable.add("potato")
Vegetable.add("tomato")
Vegetable.add("Beetroot")
puts "Number of elements in set are #{Vegetable.size()}"
puts Vegetable
Output
Number of elements in set are 4
#<Set: {"potato", "tomato", "brinjal", "onion"}>
Number of elements in set are 5
#<Set: {"potato", "tomato", "brinjal", "onion", "Beetroot"}>
Explanation
In the above code, after observing the output you must be thinking that the size of the set is 5 even after adding 3 more elements. This is because the elements which we were trying to add are already present in the set. It doesn’t allow the repetition of elements. You will not get an error but the duplicate elements will be discarded.