Skip to content

Commit 352ad42

Browse files
committed
Add signature for Range#count
1 parent e901a62 commit 352ad42

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

core/range.rbs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,42 @@ class Range[out Elem] < Object
977977
#
978978
def size: () -> (Integer | Float | nil)
979979

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+
9801016
# <!--
9811017
# rdoc-file=range.c
9821018
# - step(s = 1) {|element| ... } -> self

test/stdlib/Range_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,11 @@ def test_minmax
163163
assert_send_type "() { (::Integer, ::Integer) -> ::Integer } -> [::Integer, ::Integer]", (1..4), :minmax do |a, b| a.size <=> b.size end
164164
assert_send_type "() { (::Integer, ::Integer) -> ::Integer } -> [nil, nil]", [], :minmax do |a, b| a <=> b end
165165
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
166173
end

0 commit comments

Comments
 (0)