Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 63921c6

Browse files
committedJul 8, 2024·
Updated Integer
1 parent b85cd82 commit 63921c6

File tree

5 files changed

+1027
-476
lines changed

5 files changed

+1027
-476
lines changed
 

‎core/integer.rbs

+182-184
Large diffs are not rendered by default.

‎core/numeric.rbs

+109
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,115 @@
162162
class Numeric
163163
include Comparable
164164

165+
# Type alias for the `half` parameter in `round` functions.
166+
type round_half = string | :up | :down | :even | 'up' | 'down' | 'even'
167+
168+
# Interface which indicates a type implements ruby's coercion scheme.
169+
#
170+
# The `coerce` method is only used in the stdlib of Ruby when an operator (such as `Integer#+`)
171+
# doesn't know how to handle the second argument. Instead, `other_arg.coerce(the_integer)` is
172+
# called, and then operation is then run on the two return arguments (`equiv_self + equiv_other`).
173+
#
174+
# Traditionally, `_Coerce` functions return a tuple of `[MyClass.new(...), self]`, this is not
175+
# a strict requirement, and there are cases where it's useful to return other types. As such,
176+
# three generics are required to represent `_Coerce`.
177+
interface _Coerce[Other, EquivOther, EquivSelf]
178+
# Coerce `other` and `self` into types where the original operation can be performed.
179+
#
180+
# See `_Coerce` for more details.
181+
def coerce: (Other other) -> [EquivOther, EquivSelf]
182+
end
183+
184+
# Adds an `Other` to `self`, returning a `Return`.
185+
interface _OpAdd[Other, Return]
186+
# Performs the addition.
187+
def +: (Other other) -> Return
188+
end
189+
190+
# Subtracts an `Other` from `self`, returning a `Return`.
191+
interface _OpSub[Other, Return]
192+
# Performs the subtraction.
193+
def -: (Other other) -> Return
194+
end
195+
196+
# Multiplies `self` by an `Other`, returning a `Return`.
197+
interface _OpMul[Other, Return]
198+
# Performs the multiplication.
199+
def *: (Other other) -> Return
200+
end
201+
202+
# Divides `self` by an `Other`, returning a `Return`.
203+
interface _OpDiv[Other, Return]
204+
# Performs the division.
205+
def /: (Other other) -> Return
206+
end
207+
208+
# Modulos `self` by an `Other`, returning a `Return`.
209+
interface _OpMod[Other, Return]
210+
# Performs the modulus.
211+
def %: (Other other) -> Return
212+
end
213+
214+
# Exponentiates `self` by `Other`, returning a `Return`.
215+
interface _OpPow[Other, Return]
216+
# Performs the exponentiation.
217+
def **: (Other other) -> Return
218+
end
219+
220+
# Performs bitwise AND of `self` and `Other`, returning a `Return`.
221+
interface _OpAnd[Other, Return]
222+
# Performs the bitwise AND.
223+
def &: (Other other) -> Return
224+
end
225+
226+
# Performs bitwise OR of `self` and `Other`, returning a `Return`.
227+
interface _OpOr[Other, Return]
228+
# Performs the bitwise OR.
229+
def |: (Other other) -> Return
230+
end
231+
232+
# Performs bitwise XOR of `self` and `Other`, returning a `Return`.
233+
interface _OpXor[Other, Return]
234+
# Performs the bitwise XOR.
235+
def ^: (Other other) -> Return
236+
end
237+
238+
# Returns whether `self` is less than `Other`.
239+
interface _OpLt[Other]
240+
# Performs the lesser-than comparison.
241+
def <: (Other other) -> bool
242+
end
243+
244+
# Returns whether `self` is less than or equal to `Other`.
245+
interface _OpLe[Other]
246+
# Performs the lesser-than-or-equal-to comparison.
247+
def <=: (Other other) -> bool
248+
end
249+
250+
# Returns whether `self` is greater than `Other`.
251+
interface _OpGt[Other]
252+
# Performs the greater-than comparison.
253+
def >: (Other other) -> bool
254+
end
255+
256+
# Returns whether `self` is greater than or equal to `Other`.
257+
interface _OpGe[Other]
258+
# Performs the greater-than-or-equal-to comparison.
259+
def >=: (Other other) -> bool
260+
end
261+
262+
# Performs integer division of `self` by `Other`.
263+
interface _Div[Other]
264+
# Performs the integer division.
265+
def div: (Other other) -> Integer
266+
end
267+
268+
# Performs integer division of `self` by `Other`, returning the whole part and the `Remainder`.
269+
interface _Divmod[Other, Remainder]
270+
# Performs the division and modulus.
271+
def divmod: (Other other) -> [Integer, Remainder]
272+
end
273+
165274
public
166275

167276
# <!--

‎lib/rbs/unit_test/with_aliases.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def with_range(start, stop, exclude_end = false)
127127

128128
# `Range` requires `begin <=> end` to return non-nil, but doesn't actually
129129
# end up using the return value of it. This is to add that in when needed.
130-
def lower.<=>(rhs) = :not_nil unless defined? lower.<=>
130+
defined?(lower.<=>) or def lower.<=>(rhs) = :not_nil
131131

132132
# If `lower <=> rhs` is defined but nil, then that means we're going to be constructing
133133
# an illegal range (eg `3..ToInt.new(4)`). So, we need to skip yielding an invalid range

‎test/stdlib/Integer_test.rb

+704-291
Large diffs are not rendered by default.

‎test/stdlib/test_helper.rb

+31
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,37 @@ def if_ruby31(&block)
5151
end
5252

5353
module WithStdlibAliases
54+
def with_round_half(&block)
55+
%i[up down even].each do |val|
56+
block.call val
57+
with_string(val.to_s, &block)
58+
end
59+
end
60+
61+
def with_coerce(self:, method:, return_value: return_value_not_given=true)
62+
blankslate = RBS::UnitTest::Convertibles::BlankSlate
63+
64+
coercer = blankslate.new.__with_object_methods(:define_singleton_method)
65+
equiv_other = blankslate.new.__with_object_methods(:define_singleton_method)
66+
equiv_self = blankslate.new
67+
return_value = blankslate.new if return_value_not_given
68+
self_ = binding.local_variable_get(:self)
69+
70+
assert_type_fn = method(:assert_type)
71+
coercer.define_singleton_method :coerce do |other|
72+
assert_type_fn.call self_, other
73+
[equiv_other, equiv_self]
74+
end
75+
76+
assert_fn = method(:assert)
77+
equiv_other.define_singleton_method method do |other|
78+
assert_fn.call ::Object.instance_method(:equal?).bind_call(other, equiv_self)
79+
return_value
80+
end
81+
82+
yield coercer
83+
end
84+
5485
def with_timeout(seconds: 1, nanoseconds: 0)
5586
unless block_given?
5687
return RBS::UnitTest::Convertibles::WithAliases::WithEnum.new(

0 commit comments

Comments
 (0)
Please sign in to comment.