Skip to content

Commit 4691c6d

Browse files
committed
finished array
1 parent be27cac commit 4691c6d

2 files changed

Lines changed: 317 additions & 87 deletions

File tree

core/array.rbs

Lines changed: 92 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ class Array[unchecked out E]
723723
#
724724
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
725725
#
726-
def -: (array[untyped] other_array) -> Array[E] # TODO: `where E: _Eql?[T]`
726+
def -: (array[untyped] other_array) -> Array[E] # TODO: `where E: _Eql?[T]`
727727

728728
# <!--
729729
# rdoc-file=array.c
@@ -1233,9 +1233,8 @@ class Array[unchecked out E]
12331233
#
12341234
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
12351235
#
1236-
def bsearch: () -> ::Enumerator[E, E?]
1237-
| () { (E) -> (true | false) } -> E?
1238-
| () { (E) -> ::Integer } -> E?
1236+
def bsearch: () { (E element) -> (Numeric | bool?) } -> E?
1237+
| () -> Enumerator[E, E?]
12391238

12401239
# <!--
12411240
# rdoc-file=array.c
@@ -1249,8 +1248,8 @@ class Array[unchecked out E]
12491248
#
12501249
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
12511250
#
1252-
def bsearch_index: () { (E) -> (true | false) } -> ::Integer?
1253-
| () { (E) -> ::Integer } -> ::Integer?
1251+
def bsearch_index: () { (E element) -> (Numeric | bool?) } -> Integer?
1252+
| () -> Enumerator[E, Integer?]
12541253

12551254
# <!--
12561255
# rdoc-file=array.c
@@ -1284,8 +1283,8 @@ class Array[unchecked out E]
12841283
# Related: #collect!; see also [Methods for
12851284
# Converting](rdoc-ref:Array@Methods+for+Converting).
12861285
#
1287-
def collect: [U] () { (E item) -> U } -> ::Array[U]
1288-
| () -> ::Enumerator[E, ::Array[untyped]]
1286+
def collect: [T] () { (E item) -> T } -> Array[T]
1287+
| () -> Enumerator[E, Array[untyped]]
12891288

12901289
# <!--
12911290
# rdoc-file=array.c
@@ -1305,8 +1304,8 @@ class Array[unchecked out E]
13051304
# Related: #collect; see also [Methods for
13061305
# Converting](rdoc-ref:Array@Methods+for+Converting).
13071306
#
1308-
def collect!: () { (E item) -> E } -> self
1309-
| () -> ::Enumerator[E, self]
1307+
def collect!: () { (E element) -> E } -> self
1308+
| () -> Enumerator[E, self]
13101309

13111310
# <!--
13121311
# rdoc-file=array.c
@@ -1350,8 +1349,8 @@ class Array[unchecked out E]
13501349
# Related: Array#permutation; see also [Methods for
13511350
# Iterating](rdoc-ref:Array@Methods+for+Iterating).
13521351
#
1353-
def combination: (int n) { (::Array[E]) -> void } -> self
1354-
| (int n) -> ::Enumerator[::Array[E], self]
1352+
def combination: (int count) { (Array[E] combination) -> void } -> self
1353+
| (int count) -> Enumerator[Array[E], self]
13551354

13561355
# <!--
13571356
# rdoc-file=array.c
@@ -1461,8 +1460,8 @@ class Array[unchecked out E]
14611460
#
14621461
# Related: see [Methods for Iterating](rdoc-ref:Array@Methods+for+Iterating).
14631462
#
1464-
def cycle: (?int? n) { (E) -> void } -> nil
1465-
| (?int? n) -> ::Enumerator[E, nil]
1463+
def cycle: (?int? n) { (E element) -> void } -> nil
1464+
| (?int? n) -> Enumerator[E, nil]
14661465

14671466
# <!--
14681467
# rdoc-file=array.c
@@ -1555,7 +1554,24 @@ class Array[unchecked out E]
15551554
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
15561555
#
15571556
def delete_if: () { (E item) -> boolish } -> self
1558-
| () -> ::Enumerator[E, self]
1557+
| () -> Enumerator[E, self]
1558+
1559+
# <!-- rdoc-file=array.c -->
1560+
# Returns the first element for which the block returns a truthy value.
1561+
#
1562+
# With a block given, calls the block with successive elements of the array;
1563+
# returns the first element for which the block returns a truthy value:
1564+
#
1565+
# [1, 3, 5].find {|element| element > 2} # => 3
1566+
#
1567+
# If no such element is found, calls `if_none_proc` and returns its return
1568+
# value.
1569+
#
1570+
# [1, 3, 5].find(proc {-1}) {|element| element > 12} # => -1
1571+
#
1572+
# With no block given, returns an Enumerator.
1573+
#
1574+
alias detect find
15591575

15601576
# <!--
15611577
# rdoc-file=array.c
@@ -1634,8 +1650,8 @@ class Array[unchecked out E]
16341650
#
16351651
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
16361652
#
1637-
def drop_while: () { (E obj) -> boolish } -> ::Array[E]
1638-
| () -> ::Enumerator[E, ::Array[E]]
1653+
def drop_while: () { (E element) -> boolish } -> Array[E]
1654+
| () -> Enumerator[E, Array[E]]
16391655

16401656
# <!--
16411657
# rdoc-file=array.c
@@ -1668,8 +1684,8 @@ class Array[unchecked out E]
16681684
#
16691685
# Related: see [Methods for Iterating](rdoc-ref:Array@Methods+for+Iterating).
16701686
#
1671-
def each: () -> ::Enumerator[E, self]
1672-
| () { (E item) -> void } -> self
1687+
def each: () { (E element) -> void } -> self
1688+
| () -> Enumerator[E, self]
16731689

16741690
# <!--
16751691
# rdoc-file=array.c
@@ -1703,8 +1719,8 @@ class Array[unchecked out E]
17031719
#
17041720
# Related: see [Methods for Iterating](rdoc-ref:Array@Methods+for+Iterating).
17051721
#
1706-
def each_index: () { (::Integer index) -> void } -> self
1707-
| () -> ::Enumerator[::Integer, self]
1722+
def each_index: () { (Integer index) -> void } -> self
1723+
| () -> Enumerator[Integer, self]
17081724

17091725
# <!--
17101726
# rdoc-file=array.c
@@ -1996,8 +2012,8 @@ class Array[unchecked out E]
19962012
#
19972013
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
19982014
#
1999-
def fill: (E obj, ?int? start, ?int? length) -> self
2000-
| (E obj, range[int?] range) -> self
2015+
def fill: (E object, ?int? start, ?int? length) -> self
2016+
| (E object, range[int?] range) -> self
20012017
| (?int? start, ?int? length) { (Integer index) -> E } -> self
20022018
| (range[int?] range) { (Integer index) -> E } -> self
20032019

@@ -2014,8 +2030,7 @@ class Array[unchecked out E]
20142030
#
20152031
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
20162032
#
2017-
def filter: () { (E item) -> boolish } -> ::Array[E]
2018-
| () -> ::Enumerator[E, ::Array[E]]
2033+
alias filter select
20192034

20202035
# <!-- rdoc-file=array.c -->
20212036
# With a block given, calls the block with each element of `self`; removes from
@@ -2032,8 +2047,7 @@ class Array[unchecked out E]
20322047
#
20332048
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
20342049
#
2035-
def filter!: () { (E item) -> boolish } -> self?
2036-
| () -> ::Enumerator[E, self?]
2050+
alias filter! select!
20372051

20382052
# <!--
20392053
# rdoc-file=array.c
@@ -2055,9 +2069,9 @@ class Array[unchecked out E]
20552069
# With no block given, returns an Enumerator.
20562070
#
20572071
def find: () { (E) -> boolish } -> E?
2058-
| () -> ::Enumerator[E, E?]
2059-
| [T] (Enumerable::_NotFound[T] ifnone) { (E) -> boolish } -> (E | T)
2060-
| [T] (Enumerable::_NotFound[T] ifnone) -> ::Enumerator[E, E | T]
2072+
| () -> Enumerator[E, E?]
2073+
| [T] (Enumerable::_NotFound[T]? ifnone) { (E) -> boolish } -> (E | T)
2074+
| [T] (Enumerable::_NotFound[T]? ifnone) -> Enumerator[E, E | T]
20612075

20622076
# <!--
20632077
# rdoc-file=array.c
@@ -2090,9 +2104,9 @@ class Array[unchecked out E]
20902104
#
20912105
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
20922106
#
2093-
def find_index: (untyped obj) -> ::Integer?
2094-
| () { (E item) -> boolish } -> ::Integer?
2095-
| () -> ::Enumerator[E, ::Integer?]
2107+
def find_index: (untyped object) -> Integer? # TODO: _Equal
2108+
| () { (E element) -> boolish } -> Integer?
2109+
| () -> Enumerator[E, Integer?]
20962110

20972111
# <!--
20982112
# rdoc-file=array.rb
@@ -2194,6 +2208,24 @@ class Array[unchecked out E]
21942208
#
21952209
def flatten!: (?int? level) -> self?
21962210

2211+
# <!--
2212+
# rdoc-file=array.c
2213+
# - freeze -> self
2214+
# -->
2215+
# Freezes `self` (if not already frozen); returns `self`:
2216+
#
2217+
# a = []
2218+
# a.frozen? # => false
2219+
# a.freeze
2220+
# a.frozen? # => true
2221+
#
2222+
# No further changes may be made to `self`; raises FrozenError if a change is
2223+
# attempted.
2224+
#
2225+
# Related: Kernel#frozen?.
2226+
#
2227+
def freeze: () -> self
2228+
21972229
# <!--
21982230
# rdoc-file=array.c
21992231
# - hash -> integer
@@ -2387,8 +2419,8 @@ class Array[unchecked out E]
23872419
#
23882420
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
23892421
#
2390-
def keep_if: () { (E item) -> boolish } -> self
2391-
| () -> ::Enumerator[E, self]
2422+
def keep_if: () { (E element) -> boolish } -> self
2423+
| () -> Enumerator[E, self]
23922424

23932425
# <!--
23942426
# rdoc-file=array.rb
@@ -2430,7 +2462,7 @@ class Array[unchecked out E]
24302462
#
24312463
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
24322464
#
2433-
def length: () -> ::Integer
2465+
def length: () -> Integer
24342466

24352467
# <!-- rdoc-file=array.c -->
24362468
# With a block given, calls the block with each element of `self`; returns a new
@@ -2713,8 +2745,8 @@ class Array[unchecked out E]
27132745
#
27142746
# Related: [Methods for Iterating](rdoc-ref:Array@Methods+for+Iterating).
27152747
#
2716-
def permutation: (?int n) -> ::Enumerator[::Array[E], ::Array[E]]
2717-
| (?int n) { (::Array[E] p) -> void } -> ::Array[E]
2748+
def permutation: (?int? count) { (Array[E] permutation) -> void } -> self
2749+
| (?int? count) -> Enumerator[Array[E], self]
27182750

27192751
# <!--
27202752
# rdoc-file=array.c
@@ -2896,7 +2928,7 @@ class Array[unchecked out E]
28962928
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
28972929
#
28982930
def reject!: () { (E item) -> boolish } -> self?
2899-
| () -> ::Enumerator[E, self?]
2931+
| () -> Enumerator[E, self?]
29002932

29012933
# <!--
29022934
# rdoc-file=array.c
@@ -2935,8 +2967,8 @@ class Array[unchecked out E]
29352967
#
29362968
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
29372969
#
2938-
def repeated_combination: (int n) { (::Array[E] c) -> void } -> self
2939-
| (int n) -> ::Enumerator[::Array[E], self]
2970+
def repeated_combination: (int size) { (Array[E] combination) -> void } -> self
2971+
| (int size) -> Enumerator[Array[E], self]
29402972

29412973
# <!--
29422974
# rdoc-file=array.c
@@ -2975,8 +3007,8 @@ class Array[unchecked out E]
29753007
#
29763008
# Related: see [Methods for Combining](rdoc-ref:Array@Methods+for+Combining).
29773009
#
2978-
def repeated_permutation: (int n) { (::Array[E] p) -> void } -> self
2979-
| (int n) -> ::Enumerator[::Array[E], self]
3010+
def repeated_permutation: (int size) { (Array[E] permutation) -> void } -> self
3011+
| (int size) -> Enumerator[Array[E], self]
29803012

29813013
# <!-- rdoc-file=array.c -->
29823014
# Replaces the elements of `self` with the elements of `other_array`, which must
@@ -3039,8 +3071,8 @@ class Array[unchecked out E]
30393071
#
30403072
# Related: see [Methods for Iterating](rdoc-ref:Array@Methods+for+Iterating).
30413073
#
3042-
def reverse_each: () { (E item) -> void } -> self
3043-
| () -> ::Enumerator[E, self]
3074+
def reverse_each: () { (E element) -> void } -> self
3075+
| () -> Enumerator[E, self]
30443076

30453077
# <!--
30463078
# rdoc-file=array.c
@@ -3063,9 +3095,9 @@ class Array[unchecked out E]
30633095
# With no block given, returns an Enumerator.
30643096
#
30653097
def rfind: () { (E) -> boolish } -> E?
3066-
| () -> ::Enumerator[E, E?]
3067-
| [T] (Enumerable::_NotFound[T] ifnone) { (E) -> boolish } -> (E | T)
3068-
| [T] (Enumerable::_NotFound[T] ifnone) -> ::Enumerator[E, E | T]
3098+
| () -> Enumerator[E, E?]
3099+
| [T] (Enumerable::_NotFound[T]? ifnone) { (E) -> boolish } -> (E | T)
3100+
| [T] (Enumerable::_NotFound[T]? ifnone) -> Enumerator[E, E | T]
30693101

30703102
# <!--
30713103
# rdoc-file=array.c
@@ -3096,9 +3128,9 @@ class Array[unchecked out E]
30963128
#
30973129
# Related: see [Methods for Querying](rdoc-ref:Array@Methods+for+Querying).
30983130
#
3099-
def rindex: (untyped obj) -> ::Integer?
3100-
| () { (E item) -> boolish } -> ::Integer?
3101-
| () -> ::Enumerator[E, ::Integer?]
3131+
def rindex: (untyped object) -> Integer? # TODO: _Equal
3132+
| () { (E element) -> boolish } -> Integer?
3133+
| () -> Enumerator[E, Integer?]
31023134

31033135
# <!--
31043136
# rdoc-file=array.c
@@ -3241,8 +3273,8 @@ class Array[unchecked out E]
32413273
#
32423274
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
32433275
#
3244-
def select: () { (E item) -> boolish } -> ::Array[E]
3245-
| () -> ::Enumerator[E, ::Array[E]]
3276+
def select: () { (E element) -> boolish } -> Array[E]
3277+
| () -> Enumerator[E, Array[E]]
32463278

32473279
# <!--
32483280
# rdoc-file=array.c
@@ -3265,8 +3297,8 @@ class Array[unchecked out E]
32653297
#
32663298
# Related: see [Methods for Deleting](rdoc-ref:Array@Methods+for+Deleting).
32673299
#
3268-
def select!: () { (E item) -> boolish } -> self?
3269-
| () -> ::Enumerator[E, self?]
3300+
def select!: () { (E element) -> boolish } -> self?
3301+
| () -> Enumerator[E, self?]
32703302

32713303
# <!--
32723304
# rdoc-file=array.c
@@ -3640,8 +3672,8 @@ class Array[unchecked out E]
36403672
#
36413673
# Related: see [Methods for Assigning](rdoc-ref:Array@Methods+for+Assigning).
36423674
#
3643-
def sort_by!: [U] () { (E obj) -> U } -> ::Array[E]
3644-
| () -> ::Enumerator[E, ::Array[E]]
3675+
def sort_by!: () { (E element) -> Comparable::_WithSpaceshipOperator } -> self # TODO: use RBS::Ops
3676+
| () -> Enumerator[E, self]
36453677

36463678
# <!--
36473679
# rdoc-file=array.c
@@ -3724,8 +3756,8 @@ class Array[unchecked out E]
37243756
#
37253757
# Related: see [Methods for Fetching](rdoc-ref:Array@Methods+for+Fetching).
37263758
#
3727-
def take_while: () { (E obj) -> boolish } -> ::Array[E]
3728-
| () -> ::Enumerator[E, ::Array[E]]
3759+
def take_while: () { (E element) -> boolish } -> Array[E]
3760+
| () -> Enumerator[E, Array[E]]
37293761

37303762
# <!--
37313763
# rdoc-file=array.c
@@ -4123,9 +4155,6 @@ class Array[unchecked out E]
41234155
#
41244156
def |: [T] (array[T] other_array) -> Array[E | T] # TODO: where E: _Eql[T]
41254157

4126-
def freeze: () -> self
4127-
alias detect find
4128-
41294158
private
41304159

41314160
# <!--
@@ -4148,7 +4177,7 @@ end
41484177

41494178
%a{deprecated: Use Array::_Rand}
41504179
interface _Rand
4151-
def rand: (::Integer max) -> ::Integer
4180+
def rand: (Integer max) -> Integer
41524181
end
41534182

41544183
interface Array::_Pattern[T]

0 commit comments

Comments
 (0)