Ruby | Set compare_by_identity() function
The compare_by_identity() is an inbuilt method in Ruby makes the set compare its elements by their identity and returns self. This method may not be supported by all subclasses of Set.
Syntax: s1.name.compare_by_identity()
Parameters: The function does not takes any parameter.
Return Value: It returns self after comparing its elements.
Example 1:
#Ruby program to illustrate the compare_by_identity() method
#requires the set
require "set"
s1
= Set[]
#Add 10 to it
s1
<< 10
#Returns self after comparing internally
puts s1.compare_by_identity()
s1
<< 'a'
#Returns self after comparing internally
puts s1.compare_by_identity()
|
Output:
Set: {10}
Set: {10, "a"}
Example 2:
#Ruby program to illustrate the clear method
#requires the set
require "set"
s1
= Set[]
#Prints s1
puts s1
#Add 10 to it
s1
<< 10
puts s1
#Clears the set and returns self
#which is printed
puts s1.clear
|
Output:
Set: {}
Set: {10}
Set: {}
Reference: https://devdocs.io/ruby~2.5/set#method-i-compare_by_identity
Last Updated :
07 Jan, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...