Skip to content
Related Articles

Related Articles

Ruby | Set compare_by_identity? function
  • Last Updated : 07 Jan, 2020

The compare_by_identity?() is an inbuilt method in Ruby returns true if the set will compare its elements by their identity.

Syntax: s1.name.compare_by_identity?()

Parameters: The function does not takes any parameter.

Return Value: It returns true if the set will compare its elements by their identity..

Example 1:






#Ruby program to illustrate the
#compare_by_identity ? method
  
#requires the set
require "set"
  
    s1
    = Set[]
  
      puts s1.compare_by_identity
    ? ()
  
#Add 10 to it
              s1
          << 10
  
             puts s1.compare_by_identity
    ? ()
  
              s1
          << 'a'
  
             puts s1.compare_by_identity
    ? ()

Output:

false
false
false

Example 2:




#Ruby program to illustrate the
#compare_by_identity ? method
  
#requires the set
require "set"
  
    s1
    = Set[1, 2, 3]
  
      puts s1.compare_by_identity
    ? ()
  
#Add 12 to it
              s1
          << 12
  
             puts s1.compare_by_identity
    ? ()

Output:

false
false

Reference: https://devdocs.io/ruby~2.5/set#method-i-compare_by_identity-3F

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :