diff --git a/core/comparable.rbs b/core/comparable.rbs index bffa53b9c..53d4d488a 100644 --- a/core/comparable.rbs +++ b/core/comparable.rbs @@ -167,5 +167,13 @@ interface Comparable::_WithSpaceshipOperator # If `other` is less than `self`, it returns a positive Integer. # If no comparison is defined with `other` and `self`, it returns `nil`. # - def <=>: (untyped other) -> Integer? + def <=>: (untyped other) -> Comparable::_CompareToZero? +end + +# This interface indicates a type is comparable against zero. +# +interface Comparable::_CompareToZero + def <: (0) -> boolish + + def >: (0) -> boolish end diff --git a/test/stdlib/Comparable_test.rb b/test/stdlib/Comparable_test.rb index 6284cc9ff..480063e77 100644 --- a/test/stdlib/Comparable_test.rb +++ b/test/stdlib/Comparable_test.rb @@ -3,11 +3,17 @@ class ComparableTest < Test::Unit::TestCase include TestHelper + class ComparableWithZero + def initialize(value) = @value = value + def <(zero) = @value < zero + def >(zero) = @value > zero + end + class Test include Comparable def <=>(other) - rand(2) - 1 + ComparableWithZero.new rand(2) - 1 end end