From c4b61473ba1ae7c56c87a97c29d812fa1f50ba5f Mon Sep 17 00:00:00 2001 From: SamW Date: Mon, 3 Nov 2025 21:49:21 -0800 Subject: [PATCH] [Comparable] Add in _CompareToZero --- core/comparable.rbs | 10 +++++++++- test/stdlib/Comparable_test.rb | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) 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