Ruby | Comparable Module

In Ruby, the mixin of Comparable is used by the class whose objects may be ordered. The class must be defined using an operator which compare the receiver against another object. It will return -1, 0, and 1 depending upon the receiver. If the receiver is less than another object, then it returns -1, if the receiver is equal to another object, then it returns 0. If the receiver is greater than another object, then it returns 1. Comparable module use <=> to implement the conventional comparison operators(<, <=, ==, >=, and >) and the method between?

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program to illustrate 
# comparable module
  
class Geeksforgeeks
      
# include comparable module
include Comparable
attr :name
      
    def <=>(other_name)
        name.length <=> other_name.name.length
    end
      
    def initialize(name)
        @name = name
    end
end
  
# creating objects
a1 = Geeksforgeeks.new("G")
a2 = Geeksforgeeks.new([3, 5])
a3 = Geeksforgeeks.new("geeks")
  
# using comparable operator
p a1 < a2 
  
# using between? method
p a2.between?(a1, a3) 
p a3.between?(a1, a2) 

chevron_right


Output:



true
true
false

Instance Method

  • < : It compares two objects based on the receiver’s method and return true if it return -1 otherwise return false.
    obj<other_obj
  • <= : It compares two objects based on the receiver’s method and return true if it return -1 or 0 otherwise return false.
    obj<=other_obj
  • == : It compares two objects based on the receiver’s method and return true if it return 0 otherwise return false.
    obj==other_obj
  • > : It compares two objects based on the receiver’s method and return true if it return 1 otherwise return false.
    obj>other_obj
  • >= : It compares two objects based on the receiver’s method and return true if it return 1 or 0 otherwise return false.
    obj>=other_obj
  • Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Ruby program to illustrate 
    # use of comparisons
       
    # defining class
    class Geeksforgeeks
       
    # include comparable module
    include Comparable
    attr :name
          
        def <=>(other_name)
            name.length <=> other_name.name.length
        end
          
        def initialize(name)
            @name = name
        end
    end
       
    # creating objects
    a1 = Geeksforgeeks.new("G")
    a2 = Geeksforgeeks.new("geeks")
       
       
    # using < operator
    p a1 < a2 
      
    # using <= operator
    p a1 <= a2 
      
    # using == operator
    p a1 == a2 
      
    # using >= operator
    p a1 >= a2 
      
    # using > operator
    p a1 > a2 

    chevron_right

    
    

    Output:

    true
    true
    false
    false
    false
    
  • between? : This method returns false if the obj <=> min is less than or if obj <=> max is greater than zero. Otherwise, it returns true.
    obj.between?(min, max)

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Ruby program to illustrate 
    # use of between?  method
      
    # using between? method
    p 7.between?(2, 6
    p 'geeks'.between?('geeks', 'gfg')

    chevron_right

    
    

    Output:

    false
    true
    

Reference: https://ruby-doc.org/core-2.2.0/Comparable.html



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.