Skip to content

Commit 63cc28b

Browse files
authored
feat: Array を定義 (#46)
* chore: util インターフェースを buildin.rbs に移動 * feat: `Array` を定義 * chore: テストを追加 * chore: 不要な unchecked を削除 * feat: mrblib から提供される `Array` のメソッドを定義 * chore: テストを追加 * feat: `Enumerable` の `TODO: Array` に対応 * feat: `Object` の `TODO: Array` に対応 * feat: `Symbol` の `TODO: Array` に対応 * feat: `String` の `TODO: Array` に対応 * chore: Rubocop のルールを調整
1 parent f9955af commit 63cc28b

11 files changed

Lines changed: 331 additions & 20 deletions

File tree

core/array.rbs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
class Array[unchecked out Elem] < Object
2+
include Enumerable[Elem]
3+
4+
interface _Pattern[T]
5+
def ===: (T other) -> bool
6+
end
7+
8+
def initialize: () -> void
9+
| (Integer size, ?Elem val) -> void
10+
11+
def self.[]: [T](*T item) -> Array[T]
12+
13+
def +: [T](Array[T] other) -> Array[Elem | T]
14+
15+
def []: %a{implicitly-returns-nil} (Integer nth) -> Elem
16+
| (Range[Integer?] range) -> Array[Elem]?
17+
| (Integer start, Integer length) -> Array[Elem]?
18+
19+
alias at []
20+
21+
def []=: (Integer nth, Elem val) -> Elem
22+
| (Integer start, Integer length, Elem val) -> Elem
23+
| (Integer start, Integer length, Array[Elem] val) -> Array[Elem]
24+
25+
def clear: () -> self
26+
27+
def difference: (*Array[untyped] other) -> Array[Elem]
28+
29+
alias - difference
30+
31+
def delete_at: %a{implicitly-returns-nil} (Integer index) -> Elem
32+
33+
def empty?: () -> bool
34+
35+
def size: () -> Integer
36+
37+
alias length size
38+
39+
alias count size
40+
41+
def include?: (Elem val) -> bool
42+
43+
def &: (Array[untyped] other) -> Array[Elem]
44+
45+
def |: [T](Array[T] other) -> Array[Elem | T]
46+
47+
def first: %a{implicitly-returns-nil} () -> Elem
48+
49+
def last: %a{implicitly-returns-nil} () -> Elem
50+
51+
def push: (Elem val) -> self
52+
53+
alias << push
54+
55+
def pop: () -> Elem?
56+
| (Integer n) -> Array[Elem]
57+
58+
def shift: () -> Elem?
59+
| (Integer n) -> Array[Elem]
60+
61+
def unshift: (Elem val) -> self
62+
63+
def dup: () -> Array[Elem]
64+
65+
def min: %a{implicitly-returns-nil} () -> Elem
66+
67+
def max: %a{implicitly-returns-nil} () -> Elem
68+
69+
def minmax: () -> [Elem?, Elem?]
70+
71+
def uniq: () -> Array[Elem]
72+
73+
def uniq!: () -> self?
74+
75+
def reverse: () -> Array[Elem]
76+
77+
def reverse!: () -> self
78+
79+
def inspect: () -> String
80+
def self.inspect: () -> String
81+
82+
alias to_s inspect
83+
alias self.to_s self.inspect
84+
85+
def join: (?_ToS separator) -> String
86+
87+
def all?: () -> bool
88+
| (_Pattern[Elem] pattern) -> bool
89+
| () { (Elem item) -> boolish } -> bool
90+
91+
def any?: () -> bool
92+
| (_Pattern[Elem] pattern) -> bool
93+
| () { (Elem item) -> boolish } -> bool
94+
95+
def collect!: () { (Elem item) -> Elem } -> self
96+
97+
alias map! collect!
98+
99+
def delete_if: () { (Elem item) -> boolish } -> self
100+
101+
def each: () { (Elem item) -> void } -> self
102+
103+
def each_index: () { (Integer index) -> void } -> self
104+
105+
def index: (untyped val) -> Integer?
106+
| () { (Elem item) -> boolish } -> Integer?
107+
108+
alias find_index index
109+
110+
def none?: () -> bool
111+
| (_Pattern[Elem] pattern) -> bool
112+
| () { (Elem item) -> boolish } -> bool
113+
114+
def reject!: () { (Elem item) -> boolish } -> self?
115+
116+
def reject: () { (Elem item) -> boolish } -> Array[Elem]
117+
118+
def reverse_each: () { (Elem item) -> void } -> self
119+
120+
def select: () { (Elem item) -> boolish } -> Array[Elem]
121+
122+
alias filter select
123+
124+
def select!: () { (Elem item) -> boolish } -> self?
125+
126+
alias filter! select!
127+
128+
def sort!: () -> self
129+
| () { (Elem a, Elem b) -> Integer } -> self
130+
131+
def sort: () -> Array[Elem]
132+
| () { (Elem a, Elem b) -> Integer } -> Array[Elem]
133+
end

core/builtin.rbs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# rubocop:disable RBS/Lint/TopLevelInterface
2+
# rubocop:disable RBS/Lint/TopLevelTypeAlias
3+
4+
interface _ToS
5+
def to_s: () -> String
6+
end
7+
8+
interface _Each[out T]
9+
def each: () { (T) -> void } -> void
10+
end
11+
12+
type boolish = top
13+
14+
# rubocop:enable all

core/enumerable.rbs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
module Enumerable[unchecked out Elem] : _Each[Elem]
2-
interface _Each[unchecked out T]
3-
def each: () { (T) -> void } -> void
4-
end
2+
def collect: [T]() { (Elem item) -> T } -> Array[T]
53

6-
# TODO: Array
7-
# def collect: [T]() { (Elem item) -> T } -> Array[T]
8-
#
9-
# alias map collect
4+
alias map collect
105

116
def each_with_index: () { (Elem item, Integer index) -> void } -> self
127
end

core/object.rbs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class Object < BasicObject
3939
def nil?: () -> bool
4040
def self.nil?: () -> bool
4141

42-
# TODO: Array
4342
def p: () -> nil
4443
| [T](T arg0) -> T
44+
| [T](*T args) -> Array[T]
4545

4646
def print: (*untyped args) -> nil
4747

@@ -65,8 +65,7 @@ class Object < BasicObject
6565
alias extend include
6666
alias self.extend self.include
6767

68-
# TODO: Array
69-
# def self.constants: (?bool inherit) -> Array[Symbol]
68+
def self.constants: (?bool inherit) -> Array[Symbol]
7069

7170
def self.public: () -> void
7271

core/string.rbs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ class String < Object
6262
def slice!: (Integer nth) -> String?
6363
| (Integer nth, Integer len) -> String?
6464

65-
# TODO: Array
66-
# def split: (String? sep, ?Integer limit) -> Array[String]
65+
def split: (?String? sep, ?Integer limit) -> Array[String]
6766

6867
def lstrip: () -> String
6968

@@ -91,8 +90,7 @@ class String < Object
9190

9291
def include?: (String) -> bool
9392

94-
# TODO: Array
95-
# def bytes: () -> Array[Integer]
93+
def bytes: () -> Array[Integer]
9694

9795
def upcase: () -> String
9896

core/symbol.rbs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
class Symbol < Object
2-
# TODO: Array
3-
# def self.all_symbols: () -> Array[Symbol]
2+
def self.all_symbols: () -> Array[Symbol]
43

54
def inspect: () -> String
65
def self.inspect: () -> String

test/array.rb

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# frozen_string_literal: true
2+
3+
# rubocop:disable Lint/Void
4+
# rubocop:disable Style/EmptyLiteral
5+
# rubocop:disable Style/RedundantArrayConstructor
6+
# rubocop:disable Style/NumericPredicate
7+
# rubocop:disable Style/EvenOdd
8+
9+
arr = [1, 2, 3]
10+
11+
Array.new
12+
Array.new 10
13+
Array.new 10, ''
14+
15+
Array[]
16+
Array[1, 2, 3]
17+
18+
arr + []
19+
arr + [2]
20+
arr + ['2']
21+
22+
arr[0]
23+
arr[0..1]
24+
arr[0..]
25+
arr[...2]
26+
arr[nil...]
27+
arr[0, 2]
28+
29+
arr.at 0
30+
arr.at 0..1
31+
arr.at 0..nil
32+
arr.at nil...2
33+
arr.at nil...nil
34+
arr.at 0, 2
35+
36+
arr[0] = 1
37+
arr[0, 2] = 1
38+
arr[0, 2] = [2, 3, 4]
39+
40+
arr.clear
41+
42+
arr.difference []
43+
arr.difference [], [1], ['a']
44+
45+
arr - []
46+
arr.-([], [1], ['a'])
47+
48+
arr.delete_at 0
49+
50+
arr.empty?
51+
52+
arr.size
53+
54+
arr.length
55+
56+
arr.count
57+
58+
arr.include? 0
59+
60+
arr & []
61+
arr & [1, 2]
62+
arr & [1, 'a', nil]
63+
64+
arr | []
65+
arr | [4]
66+
arr | [4, 'a', nil]
67+
68+
arr.first
69+
70+
arr.last
71+
72+
arr.push 1
73+
74+
arr << 1
75+
76+
arr.pop
77+
arr.pop 2
78+
79+
arr.shift
80+
arr.shift 2
81+
82+
arr.unshift 1
83+
84+
arr.dup
85+
86+
arr.min
87+
88+
arr.max
89+
90+
arr.minmax
91+
92+
arr.uniq
93+
94+
arr.uniq!
95+
96+
arr.reverse
97+
98+
arr.reverse!
99+
100+
arr.inspect
101+
Array.inspect
102+
103+
arr.to_s
104+
Array.to_s
105+
106+
arr.join
107+
arr.join ','
108+
arr.join 123
109+
110+
arr.all?
111+
arr.all? Integer
112+
arr.all? { |item| item > 0 }
113+
114+
arr.any?
115+
arr.any? String
116+
arr.any? { |item| item == 0 }
117+
118+
arr.collect! { |item| item**2 }
119+
120+
arr.map! { |item| item**2 }
121+
122+
arr.delete_if { |item| item % 2 == 0 }
123+
124+
arr.each { |item| item }
125+
126+
arr.each_index { |index| index }
127+
128+
arr.index 1
129+
arr.index { |item| item > 0 }
130+
131+
arr.find_index 1
132+
arr.find_index { |item| item > 0 }
133+
134+
arr.none?
135+
arr.none? String
136+
arr.none? { |item| item == 0 }
137+
138+
arr.reject! { |item| item == 0 }
139+
140+
arr.reject { |item| item == 0 }
141+
142+
arr.reverse_each { |item| item }
143+
144+
arr.select { |item| item % 2 == 1 }
145+
146+
arr.filter { |item| item % 2 == 1 }
147+
148+
arr.select! { |item| item % 2 == 1 }
149+
150+
arr.filter! { |item| item % 2 == 1 }
151+
152+
arr.sort!
153+
arr.sort! { |a, b| b - a }
154+
155+
arr.sort
156+
arr.sort { |a, b| b - a }
157+
158+
# rubocop:enable all

test/enumerable.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# rubocop:disable Lint/Void
44
# rubocop:disable Style/Documentation
5+
# rubocop:disable Style/SymbolProc
56

67
class EnumerableTestClass
78
include Enumerable
@@ -14,10 +15,11 @@ def each
1415
a = EnumerableTestClass.new
1516
a.each { |item| item }
1617

17-
# TODO: Array
18-
# a.collect { |item| item }
19-
#
20-
# a.map { |item| item }
18+
a.collect { |item| item }
19+
a.collect { |item| item.size }
20+
21+
a.map { |item| item }
22+
a.map { |item| item.size }
2123

2224
a.each_with_index do |item, index|
2325
item

0 commit comments

Comments
 (0)