Skip to content

Commit 87ab8ac

Browse files
authored
Merge pull request #2697 from sampersand/sampersand/2025-11-03/add-comparable-with-zero
[Comparable] Add in Comparable::_CompareToZero
2 parents 6625598 + 2cef201 commit 87ab8ac

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

core/comparable.rbs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,13 @@ interface Comparable::_WithSpaceshipOperator
174174
# If `other` is less than `self`, it returns a positive Integer.
175175
# If no comparison is defined with `other` and `self`, it returns `nil`.
176176
#
177-
def <=>: (untyped other) -> Integer?
177+
def <=>: (untyped other) -> Comparable::_CompareToZero?
178+
end
179+
180+
# This interface indicates a type is comparable against zero.
181+
#
182+
interface Comparable::_CompareToZero
183+
def <: (0) -> boolish
184+
185+
def >: (0) -> boolish
178186
end

test/stdlib/Comparable_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
class ComparableTest < Test::Unit::TestCase
44
include TestHelper
55

6+
class ComparableWithZero
7+
def initialize(value) = @value = value
8+
def <(zero) = @value < zero
9+
def >(zero) = @value > zero
10+
end
11+
612
class Test
713
include Comparable
814

915
def <=>(other)
10-
rand(2) - 1
16+
ComparableWithZero.new rand(2) - 1
1117
end
1218
end
1319

0 commit comments

Comments
 (0)