Reputation:
I want to know what is the meaning of def <=>(other)
in ruby methods. I want to know what is the <=>
in ruby method.
Upvotes: 2
Views: 125
Reputation: 12578
<=>
is not "in" Ruby method, #<=>
is a Ruby method. This method is used for comparable objects (members of ordered sets) to easily gain implementation of #<
, #>
, #==
etc. methods by including Comparable
mixin.
class GradeInFiveLevelScale
include Comparable
attr_reader :grade
def initialize grade; @grade = grade end
def <=> other; other.grade <=> grade end
def to_s; grade.to_s end
end
a = GradeInFiveLevelScale.new 1
b = GradeInFiveLevelScale.new 1
c = GradeInFiveLevelScale.new 3
a > b #=> false
a >= b #=> true
a > c #=> true
Upvotes: 1