Skip to content

Commit b0c70bb

Browse files
authored
Merge pull request #2562 from ksss/range
Add documentation for `Range#{minmax,count,to_a,entries}`
2 parents b26b489 + f2f19cb commit b0c70bb

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

core/range.rbs

Lines changed: 104 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
@@ -932,6 +977,42 @@ class Range[out Elem] < Object
932977
#
933978
def size: () -> (Integer | Float | nil)
934979

980+
# <!--
981+
# rdoc-file=range.c
982+
# - count -> integer
983+
# - count(object) -> integer
984+
# - count {|element| ... } -> integer
985+
# -->
986+
# Returns the count of elements, based on an argument or block criterion, if
987+
# given.
988+
#
989+
# With no argument and no block given, returns the number of elements:
990+
#
991+
# (1..4).count # => 4
992+
# (1...4).count # => 3
993+
# ('a'..'d').count # => 4
994+
# ('a'...'d').count # => 3
995+
# (1..).count # => Infinity
996+
# (..4).count # => Infinity
997+
#
998+
# With argument `object`, returns the number of `object` found in `self`, which
999+
# will usually be zero or one:
1000+
#
1001+
# (1..4).count(2) # => 1
1002+
# (1..4).count(5) # => 0
1003+
# (1..4).count('a') # => 0
1004+
#
1005+
# With a block given, calls the block with each element; returns the number of
1006+
# elements for which the block returns a truthy value:
1007+
#
1008+
# (1..4).count {|element| element < 3 } # => 2
1009+
#
1010+
# Related: Range#size.
1011+
#
1012+
def count: () -> (Integer | Float)
1013+
| (untyped) -> Integer
1014+
| () { (Elem) -> boolish } -> Integer
1015+
9351016
# <!--
9361017
# rdoc-file=range.c
9371018
# - step(s = 1) {|element| ... } -> self
@@ -1097,4 +1178,27 @@ class Range[out Elem] < Object
10971178
# Related: Range#cover?.
10981179
#
10991180
def member?: (untyped obj) -> bool
1181+
1182+
# <!--
1183+
# rdoc-file=range.c
1184+
# - to_a -> array
1185+
# -->
1186+
# Returns an array containing the elements in `self`, if a finite collection;
1187+
# raises an exception otherwise.
1188+
#
1189+
# (1..4).to_a # => [1, 2, 3, 4]
1190+
# (1...4).to_a # => [1, 2, 3]
1191+
# ('a'..'d').to_a # => ["a", "b", "c", "d"]
1192+
#
1193+
def to_a: ...
1194+
1195+
# <!-- rdoc-file=range.c -->
1196+
# Returns an array containing the elements in `self`, if a finite collection;
1197+
# raises an exception otherwise.
1198+
#
1199+
# (1..4).to_a # => [1, 2, 3, 4]
1200+
# (1...4).to_a # => [1, 2, 3]
1201+
# ('a'..'d').to_a # => ["a", "b", "c", "d"]
1202+
#
1203+
alias entries to_a
11001204
end

test/stdlib/Range_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,22 @@ 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
166+
167+
def test_count
168+
assert_send_type "() -> ::Integer", (1..4), :count
169+
assert_send_type "() -> ::Float", (1..), :count
170+
assert_send_type "(::Integer) -> ::Integer", (1..4), :count, 2
171+
assert_send_type "() { (::Integer) -> boolish } -> ::Integer", (1..4), :count do |element| element < 3 end
172+
end
173+
174+
def test_to_a
175+
assert_send_type "() -> ::Array[::Integer]", (1..4), :to_a
176+
end
159177
end

0 commit comments

Comments
 (0)