Home »
Ruby Tutorial
Ruby Array.uniq! Method
By IncludeHelp Last updated : December 01, 2024
In this article, we will study about Array.uniq! Method. You all must be thinking the method must be doing something related to finding unique elements or objects from the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.
Description and Usage
This method is a public instance method and defined for the Array class in Ruby's library. This method works in a way that it returns a new array that contains all the unique elements from the self Array instance. If you are providing a block along with the method at the time of its invocation then it will use the return value of block for the comparison. This method will create an impact on the actual arrangement of objects in the self Array because this method belongs to the category of destructive methods where the changes created by the method are non-temporary or permanent.
Syntax
array_instance.uniq! -> Array or nil
or
array_instance.uniq!{|item ...| block}-> new_array
Parameters
This method does not take any argument. Instead, you will have to pass a block if you want to find unique objects on the basis of certain criteria.
Example 1
=begin
Ruby program to demonstrate uniq! method
=end
# array declaration
table = ["Subha","Sham","Sham","Vivek","Me","Amisha","Zain","Subha","Vivek"]
puts "Array uniq! implementation"
new_arr = table.uniq!
puts "Array after finding unique: #{new_arr}"
puts "Original Array instance: #{table}"
Output
Array uniq! implementation
Array after finding unique: ["Subha", "Sham", "Vivek", "Me", "Amisha", "Zain"]
Original Array instance: ["Subha", "Sham", "Vivek", "Me", "Amisha", "Zain"]
Explanation
In the above code, you can observe that we are finding unique elements in the Array instance with the help of Array.uniq! method. In the new Array, we have all the objects which are unique in the self Array. This method is creating an impact on the original Array instance because this method is one of the examples of destructive methods.
Example 2
=begin
Ruby program to demonstrate uniq! method
=end
# array declaration
table = [["Subha","Sham"],["Subha","Raat"],["din","dopher"]]
puts "Array uniq! implementation"
new_arr = table.uniq!{|itm| itm.first}
puts "Array after finding unique: #{new_arr}"
puts "Original Array instance: #{table}"
Output
Array uniq! implementation
Array after finding unique: [["Subha", "Sham"], ["din", "dopher"]]
Original Array instance: [["Subha", "Sham"], ["din", "dopher"]]
Explanation
In the above output, you can notice that we are calling Array.uniq! method with a block. Our self Array is a multidimensional Array that contains three sub Arrays. Though all the three Arrays are unique the method had carried out the comparison of the basis of the first object of all the three Array instances. This method is creating an impact on the original Array instance because this method is one of the examples of destructive methods.