diff --git a/core/array.rbs b/core/array.rbs index 188e0df81..ca55f0f6d 100644 --- a/core/array.rbs +++ b/core/array.rbs @@ -546,9 +546,15 @@ # given block. # %a{annotate:rdoc:source:from=array.c} -class Array[unchecked out E] < Object +class Array[unchecked out E] include Enumerable[E] + # Interface for random number generations; used with `Array#shuffle`, `Array#shuffle!`, and `Array#sample`. + interface _Rand + # Generate a random number up to, but excluding, `max` + def rand: (Integer max) -> int + end + # # Appends each argument in `objects` to `self`; returns `self`: @@ -1187,7 +1195,7 @@ class Array[unchecked out E] < Object # Related: Array#rassoc; see also [Methods for # Fetching](rdoc-ref:Array@Methods+for+Fetching). # - def assoc: (untyped) -> ::Array[untyped]? + def assoc: (untyped object) -> Array[untyped]? # TODO: where `E: _ToAry[X]`, and object: _Equal # + # Returns the first element for which the block returns a truthy value. + # + # With a block given, calls the block with successive elements of the array; + # returns the first element for which the block returns a truthy value: + # + # [1, 3, 5].find {|element| element > 2} # => 3 + # + # If no such element is found, calls `if_none_proc` and returns its return + # value. + # + # [1, 3, 5].find(proc {-1}) {|element| element > 12} # => -1 + # + # With no block given, returns an Enumerator. + # + alias detect find # # With a block given, calls the block with each element of `self`; returns a new @@ -2007,8 +2030,7 @@ class Array[unchecked out E] < Object # # Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching). # - def filter: () { (E item) -> boolish } -> ::Array[E] - | () -> ::Enumerator[E, ::Array[E]] + alias filter select # # With a block given, calls the block with each element of `self`; removes from @@ -2025,8 +2047,7 @@ class Array[unchecked out E] < Object # # Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting). # - def filter!: () { (E item) -> boolish } -> self? - | () -> ::Enumerator[E, self?] + alias filter! select! # + # Freezes `self` (if not already frozen); returns `self`: + # + # a = [] + # a.frozen? # => false + # a.freeze + # a.frozen? # => true + # + # No further changes may be made to `self`; raises FrozenError if a change is + # attempted. + # + # Related: Kernel#frozen?. + # + def freeze: () -> self # # Returns the zero-based integer index of a specified element, or `nil`. @@ -2278,7 +2317,7 @@ class Array[unchecked out E] < Object # # Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning). # - def insert: (int index, *E obj) -> self + def insert: (int index, *E objects) -> self # # With a block given, calls the block with each element of `self`; returns a new @@ -2502,10 +2541,8 @@ class Array[unchecked out E] < Object # # Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching). # - def max: %a{implicitly-returns-nil} () -> E - | %a{implicitly-returns-nil} () { (E a, E b) -> ::Integer? } -> E - | (int n) -> ::Array[E] - | (int n) { (E a, E b) -> ::Integer? } -> ::Array[E] + def max: %a{implicitly-returns-nil} () ?{ (E a, E b) -> Comparable::_CompareToZero? } -> E # TODO: where `E: Comparable::_WithSpaceshipOperator` + | (int count) ?{ (E a, E b) -> Comparable::_CompareToZero? } -> Array[E] # TODO: i dont think these shoudl have `?` in _CompareToZero. But it would make signatures nicer? # # Prepends the given `objects` to `self`: @@ -2803,10 +2844,14 @@ class Array[unchecked out E] < Object # # Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining). # - def product: () -> ::Array[[ E ]] - | [X] (::Array[X] other_ary) -> ::Array[[ E, X ]] - | [X, Y] (::Array[X] other_ary1, ::Array[Y] other_ary2) -> ::Array[[ E, X, Y ]] - | [U] (*::Array[U] other_arys) -> ::Array[::Array[E | U]] + def product: () -> Array[[ E ]] + | () { ([E] combination) -> void } -> self + | [X] (array[X] other_ary) -> Array[[ E, X ]] + | [X] (array[X] other_ary) { ([E, X] combination) -> void } -> self + | [X, Y] (array[X] other_ary1, array[Y] other_ary2) -> Array[[ E, X, Y ]] + | [X, Y] (array[X] other_ary1, array[Y] other_ary2) { ([E, X, Y] combination) -> void } -> self + | [U] (*array[U] other_arys) -> Array[Array[E | U]] + | [U] (*array[U] other_arys) { (Array[E | U] combination) -> void } -> self # # Replaces the elements of `self` with the elements of `other_array`, which must @@ -2976,7 +3021,7 @@ class Array[unchecked out E] < Object # # Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning). # - def replace: (::Array[E]) -> self + def replace: (array[E] other_array) -> self # # Returns the count of elements in `self`: @@ -3464,9 +3509,7 @@ class Array[unchecked out E] < Object # # Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching). # - def slice: %a{implicitly-returns-nil} (int index) -> E - | (int start, int length) -> ::Array[E]? - | (::Range[::Integer] range) -> ::Array[E]? + alias slice [] # # Returns the new string formed by calling method #inspect on each @@ -3797,7 +3837,7 @@ class Array[unchecked out E] < Object # # Related: see [Methods for Converting](rdoc-ref:Array@Methods+for+Converting). # - def transpose: () -> ::Array[::Array[untyped]] + def transpose: () -> Array[Array[untyped]] #