Skip to content

Commit

Permalink
Merge pull request #1921 from hjwylde/hjwylde/type-signature-updates
Browse files Browse the repository at this point in the history
Type signature updates for Enumerable, Module, Range, Array
  • Loading branch information
soutaro committed Jul 19, 2024
2 parents 9648cbd + 134d2df commit b0f5a72
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 11 deletions.
20 changes: 10 additions & 10 deletions core/array.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ class Array[unchecked out Elem] < Object
# a.at(0) # => :foo
# a.at(2) # => 2
#
def at: (int index) -> Elem?
def at: %a{implicitly-returns-nil} (int index) -> Elem

# <!--
# rdoc-file=array.c
Expand Down Expand Up @@ -1378,7 +1378,7 @@ class Array[unchecked out Elem] < Object
#
# If `index` is too small (far from zero), returns nil.
#
def delete_at: (int index) -> Elem?
def delete_at: %a{implicitly-returns-nil} (int index) -> Elem

# <!--
# rdoc-file=array.c
Expand Down Expand Up @@ -1942,7 +1942,7 @@ class Array[unchecked out Elem] < Object
#
# Related: #last.
#
def first: () -> Elem?
def first: %a{implicitly-returns-nil} () -> Elem
| (int n) -> ::Array[Elem]

# <!--
Expand Down Expand Up @@ -2241,7 +2241,7 @@ class Array[unchecked out Elem] < Object
#
# Related: #first.
#
def last: () -> Elem?
def last: %a{implicitly-returns-nil} () -> Elem
| (int n) -> ::Array[Elem]

# <!--
Expand Down Expand Up @@ -2321,8 +2321,8 @@ class Array[unchecked out Elem] < Object
#
# ['0', '00', '000'].max(2) {|a, b| a.size <=> b.size } # => ["000", "00"]
#
def max: () -> Elem?
| () { (Elem a, Elem b) -> ::Integer? } -> Elem?
def max: %a{implicitly-returns-nil} () -> Elem
| %a{implicitly-returns-nil} () { (Elem a, Elem b) -> ::Integer? } -> Elem
| (int n) -> ::Array[Elem]
| (int n) { (Elem a, Elem b) -> ::Integer? } -> ::Array[Elem]

Expand Down Expand Up @@ -3118,7 +3118,7 @@ class Array[unchecked out Elem] < Object
# a.sample(random: Random.new(1)) #=> 6
# a.sample(4, random: Random.new(1)) #=> [6, 10, 9, 2]
#
def sample: (?random: _Rand rng) -> Elem?
def sample: %a{implicitly-returns-nil} (?random: _Rand rng) -> Elem
| (int n, ?random: _Rand rng) -> ::Array[Elem]

# <!--
Expand Down Expand Up @@ -3197,7 +3197,7 @@ class Array[unchecked out Elem] < Object
#
# Related: #push, #pop, #unshift.
#
def shift: () -> Elem?
def shift: %a{implicitly-returns-nil} () -> Elem
| (int n) -> ::Array[Elem]

# <!--
Expand Down Expand Up @@ -3323,7 +3323,7 @@ class Array[unchecked out Elem] < Object
# # Raises TypeError (no implicit conversion of Symbol into Integer):
# a[:foo]
#
def slice: (int index) -> Elem?
def slice: %a{implicitly-returns-nil} (int index) -> Elem
| (int start, int length) -> ::Array[Elem]?
| (::Range[::Integer] range) -> ::Array[Elem]?

Expand Down Expand Up @@ -3393,7 +3393,7 @@ class Array[unchecked out Elem] < Object
# a.slice!(-2..2) # => ["bar", 2]
# a # => [:foo]
#
def slice!: (int index) -> Elem?
def slice!: %a{implicitly-returns-nil} (int index) -> Elem
| (int start, int length) -> ::Array[Elem]?
| (::Range[::Integer] range) -> ::Array[Elem]?

Expand Down
6 changes: 6 additions & 0 deletions core/enumerable.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,12 @@ module Enumerable[unchecked out Elem] : _Each[Elem]
#
def entries: () -> ::Array[Elem]

def enum_for: (Symbol method, *untyped, **untyped) ?{ (*untyped, **untyped) -> Integer } -> Enumerator[untyped, untyped]
| () ?{ () -> Integer } -> Enumerator[Elem, self]

%a{annotate:rdoc:skip}
alias to_enum enum_for

# <!--
# rdoc-file=enum.c
# - select {|element| ... } -> array
Expand Down
2 changes: 1 addition & 1 deletion core/module.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ class Module < Object
#
# Hello there!
#
def class_exec: [U] (*untyped args) { () -> U } -> U
def class_exec: [U, V] (*V args) { (*V args) [self: self] -> U } -> U

# <!--
# rdoc-file=object.c
Expand Down
30 changes: 30 additions & 0 deletions core/range.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,36 @@
class Range[out Elem] < Object
include Enumerable[Elem]

# <!--
# rdoc-file=range.c
# - %(n) {|element| ... } -> self
# - %(n) -> enumerator
# -->
# Iterates over the elements of `self`.
#
# With a block given, calls the block with selected elements of the range;
# returns `self`:
#
# a = []
# (1..5).%(2) {|element| a.push(element) } # => 1..5
# a # => [1, 3, 5]
# a = []
# ('a'..'e').%(2) {|element| a.push(element) } # => "a".."e"
# a # => ["a", "c", "e"]
#
# With no block given, returns an enumerator, which will be of class
# Enumerator::ArithmeticSequence if `self` is numeric; otherwise of class
# Enumerator:
#
# e = (1..5) % 2 # => ((1..5).%(2))
# e.class # => Enumerator::ArithmeticSequence
# ('a'..'e') % 2 # => #<Enumerator: ...>
#
# Related: Range#step.
#
def %: (Numeric | int n) -> Enumerator[Elem, self]
| (Numeric | int n) { (Elem element) -> void } -> self

# <!--
# rdoc-file=range.c
# - self == other -> true or false
Expand Down
14 changes: 14 additions & 0 deletions test/stdlib/Enumerable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
class EnumerableTest < StdlibTest
target Enumerable

def test_enum_for
enumerable.enum_for
enumerable.enum_for { 0 }
enumerable.enum_for(:each)
enumerable.enum_for(:each) { 0 }
end

def test_find_all
enumerable.find_all
enumerable.find_all { |x| x.even? }
Expand Down Expand Up @@ -194,6 +201,13 @@ def test_each_slice
) do end
end

def test_enum_for
assert_send_type(
"() ?{ () -> Integer } -> Enumerator[String, EnumerableTest2::TestEnumerable]",
TestEnumerable.new, :enum_for
)
end

def test_find_index
assert_send_type "() -> ::Enumerator[String, Integer?]", TestEnumerable.new,
:find_index
Expand Down
6 changes: 6 additions & 0 deletions test/stdlib/Module_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ def test_class_eval
Foo, :class_eval do nil end
end

def test_class_exec
assert_send_type '(*String) { (*String) [self: Foo] -> Integer } -> Integer',
Foo, :class_exec, '1', '2' do |*x| x.join.to_i end
end


def test_alias_method
mod = Module.new do
def foo
Expand Down
5 changes: 5 additions & 0 deletions test/stdlib/Range_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ def test_min
(1..10).min(3) { |i, j| i <=> j }
end

def test_percent
(1..10).%(2)
('A'...'Z').%(2) { |s| s.downcase }
end

def test_size
(1..10).size
('A'...'Z').size
Expand Down

0 comments on commit b0f5a72

Please sign in to comment.