Skip to content

Commit e901a62

Browse files
committed
Add sigunature for Range#minmax
1 parent b26b489 commit e901a62

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

core/range.rbs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,51 @@ class Range[out Elem] < Object
825825
#
826826
def min: ...
827827

828+
# <!--
829+
# rdoc-file=range.c
830+
# - minmax -> [object, object]
831+
# - minmax {|a, b| ... } -> [object, object]
832+
# -->
833+
# Returns a 2-element array containing the minimum and maximum value in `self`,
834+
# either according to comparison method `#<=>` or a given block.
835+
#
836+
# With no block given, returns the minimum and maximum values, using `#<=>` for
837+
# comparison:
838+
#
839+
# (1..4).minmax # => [1, 4]
840+
# (1...4).minmax # => [1, 3]
841+
# ('a'..'d').minmax # => ["a", "d"]
842+
# (-4..-1).minmax # => [-4, -1]
843+
#
844+
# With a block given, the block must return an integer:
845+
#
846+
# * Negative if `a` is smaller than `b`.
847+
# * Zero if `a` and `b` are equal.
848+
# * Positive if `a` is larger than `b`.
849+
#
850+
# The block is called `self.size` times to compare elements; returns a 2-element
851+
# Array containing the minimum and maximum values from `self`, per the block:
852+
#
853+
# (1..4).minmax {|a, b| -(a <=> b) } # => [4, 1]
854+
#
855+
# Returns `[nil, nil]` if:
856+
#
857+
# * The begin value of the range is larger than the end value:
858+
#
859+
# (4..1).minmax # => [nil, nil]
860+
# (4..1).minmax {|a, b| -(a <=> b) } # => [nil, nil]
861+
#
862+
# * The begin value of an exclusive range is equal to the end value:
863+
#
864+
# (1...1).minmax # => [nil, nil]
865+
# (1...1).minmax {|a, b| -(a <=> b) } # => [nil, nil]
866+
#
867+
# Raises an exception if `self` is a beginless or an endless range.
868+
#
869+
# Related: Range#min, Range#max.
870+
#
871+
def minmax: ...
872+
828873
# <!--
829874
# rdoc-file=range.c
830875
# - overlap?(range) -> true or false

test/stdlib/Range_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,11 @@ def test_max
156156
assert_send_type "(::Integer) -> ::Array[::Integer]", (1..4), :max, 2
157157
assert_send_type "(::Integer) { (::Integer, ::Integer) -> ::Integer } -> ::Array[::Integer]", (4..1), :max, 0 do |a, b| a <=> b end
158158
end
159+
160+
def test_minmax
161+
assert_send_type "() -> [::Integer, ::Integer]", (1..4), :minmax
162+
assert_send_type "() -> [nil, nil]", [], :minmax
163+
assert_send_type "() { (::Integer, ::Integer) -> ::Integer } -> [::Integer, ::Integer]", (1..4), :minmax do |a, b| a.size <=> b.size end
164+
assert_send_type "() { (::Integer, ::Integer) -> ::Integer } -> [nil, nil]", [], :minmax do |a, b| a <=> b end
165+
end
159166
end

0 commit comments

Comments
 (0)