steep 0.13.0 → 0.16.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +0 -3
  3. data/CHANGELOG.md +28 -0
  4. data/Rakefile +0 -13
  5. data/bin/setup +0 -2
  6. data/bin/smoke_runner.rb +0 -1
  7. data/exe/steep +0 -1
  8. data/lib/steep.rb +33 -1
  9. data/lib/steep/annotation_parser.rb +4 -4
  10. data/lib/steep/ast/buffer.rb +11 -7
  11. data/lib/steep/ast/builtin.rb +8 -0
  12. data/lib/steep/ast/types/factory.rb +124 -89
  13. data/lib/steep/cli.rb +16 -1
  14. data/lib/steep/drivers/annotations.rb +1 -1
  15. data/lib/steep/drivers/check.rb +20 -4
  16. data/lib/steep/drivers/init.rb +5 -5
  17. data/lib/steep/drivers/langserver.rb +13 -287
  18. data/lib/steep/drivers/utils/driver_helper.rb +1 -1
  19. data/lib/steep/drivers/vendor.rb +2 -2
  20. data/lib/steep/drivers/watch.rb +97 -85
  21. data/lib/steep/drivers/worker.rb +51 -0
  22. data/lib/steep/project.rb +9 -5
  23. data/lib/steep/project/completion_provider.rb +298 -0
  24. data/lib/steep/project/dsl.rb +14 -0
  25. data/lib/steep/project/file.rb +54 -47
  26. data/lib/steep/project/hover_content.rb +17 -8
  27. data/lib/steep/project/options.rb +25 -3
  28. data/lib/steep/project/target.rb +40 -24
  29. data/lib/steep/server/base_worker.rb +56 -0
  30. data/lib/steep/server/code_worker.rb +151 -0
  31. data/lib/steep/server/interaction_worker.rb +281 -0
  32. data/lib/steep/server/master.rb +196 -0
  33. data/lib/steep/server/signature_worker.rb +148 -0
  34. data/lib/steep/server/utils.rb +36 -0
  35. data/lib/steep/server/worker_process.rb +62 -0
  36. data/lib/steep/signature/errors.rb +1 -1
  37. data/lib/steep/signature/validator.rb +13 -13
  38. data/lib/steep/source.rb +1 -1
  39. data/lib/steep/type_construction.rb +1004 -727
  40. data/lib/steep/type_inference/constant_env.rb +3 -11
  41. data/lib/steep/type_inference/context.rb +8 -3
  42. data/lib/steep/type_inference/context_array.rb +111 -0
  43. data/lib/steep/type_inference/local_variable_type_env.rb +226 -0
  44. data/lib/steep/type_inference/logic.rb +130 -0
  45. data/lib/steep/type_inference/type_env.rb +5 -69
  46. data/lib/steep/typing.rb +91 -23
  47. data/lib/steep/version.rb +1 -1
  48. data/smoke/alias/Steepfile +1 -0
  49. data/smoke/alias/a.rb +1 -1
  50. data/smoke/and/Steepfile +1 -0
  51. data/smoke/array/Steepfile +1 -0
  52. data/smoke/array/b.rb +0 -2
  53. data/smoke/block/Steepfile +1 -0
  54. data/smoke/case/Steepfile +1 -0
  55. data/smoke/class/Steepfile +1 -0
  56. data/smoke/const/Steepfile +1 -0
  57. data/smoke/dstr/Steepfile +1 -0
  58. data/smoke/ensure/Steepfile +1 -0
  59. data/smoke/enumerator/Steepfile +1 -0
  60. data/smoke/extension/Steepfile +1 -0
  61. data/smoke/extension/c.rb +1 -0
  62. data/smoke/hash/Steepfile +1 -0
  63. data/smoke/hello/Steepfile +1 -0
  64. data/smoke/if/Steepfile +1 -0
  65. data/smoke/if/a.rb +1 -1
  66. data/smoke/implements/Steepfile +1 -0
  67. data/smoke/initialize/Steepfile +1 -0
  68. data/smoke/integer/Steepfile +1 -0
  69. data/smoke/interface/Steepfile +1 -0
  70. data/smoke/kwbegin/Steepfile +1 -0
  71. data/smoke/lambda/Steepfile +1 -0
  72. data/smoke/literal/Steepfile +1 -0
  73. data/smoke/map/Steepfile +1 -0
  74. data/smoke/method/Steepfile +1 -0
  75. data/smoke/module/Steepfile +1 -0
  76. data/smoke/regexp/Steepfile +1 -0
  77. data/smoke/regexp/b.rb +4 -4
  78. data/smoke/regression/Steepfile +1 -0
  79. data/smoke/rescue/Steepfile +1 -0
  80. data/smoke/rescue/a.rb +1 -1
  81. data/smoke/self/Steepfile +1 -0
  82. data/smoke/skip/Steepfile +1 -0
  83. data/smoke/stdout/Steepfile +1 -0
  84. data/smoke/super/Steepfile +1 -0
  85. data/smoke/type_case/Steepfile +1 -0
  86. data/smoke/yield/Steepfile +1 -0
  87. data/steep.gemspec +8 -8
  88. metadata +38 -138
  89. data/exe/rbs +0 -3
  90. data/exe/ruby-signature +0 -3
  91. data/vendor/ruby-signature/.github/workflows/ruby.yml +0 -27
  92. data/vendor/ruby-signature/.gitignore +0 -12
  93. data/vendor/ruby-signature/.rubocop.yml +0 -15
  94. data/vendor/ruby-signature/BSDL +0 -22
  95. data/vendor/ruby-signature/COPYING +0 -56
  96. data/vendor/ruby-signature/Gemfile +0 -6
  97. data/vendor/ruby-signature/README.md +0 -93
  98. data/vendor/ruby-signature/Rakefile +0 -66
  99. data/vendor/ruby-signature/bin/annotate-with-rdoc +0 -156
  100. data/vendor/ruby-signature/bin/console +0 -14
  101. data/vendor/ruby-signature/bin/query-rdoc +0 -103
  102. data/vendor/ruby-signature/bin/setup +0 -10
  103. data/vendor/ruby-signature/bin/sort +0 -88
  104. data/vendor/ruby-signature/bin/test_runner.rb +0 -17
  105. data/vendor/ruby-signature/docs/CONTRIBUTING.md +0 -97
  106. data/vendor/ruby-signature/docs/sigs.md +0 -148
  107. data/vendor/ruby-signature/docs/stdlib.md +0 -152
  108. data/vendor/ruby-signature/docs/syntax.md +0 -528
  109. data/vendor/ruby-signature/exe/rbs +0 -3
  110. data/vendor/ruby-signature/exe/ruby-signature +0 -7
  111. data/vendor/ruby-signature/lib/ruby/signature.rb +0 -64
  112. data/vendor/ruby-signature/lib/ruby/signature/ast/annotation.rb +0 -29
  113. data/vendor/ruby-signature/lib/ruby/signature/ast/comment.rb +0 -29
  114. data/vendor/ruby-signature/lib/ruby/signature/ast/declarations.rb +0 -391
  115. data/vendor/ruby-signature/lib/ruby/signature/ast/members.rb +0 -364
  116. data/vendor/ruby-signature/lib/ruby/signature/buffer.rb +0 -52
  117. data/vendor/ruby-signature/lib/ruby/signature/builtin_names.rb +0 -54
  118. data/vendor/ruby-signature/lib/ruby/signature/cli.rb +0 -534
  119. data/vendor/ruby-signature/lib/ruby/signature/constant.rb +0 -28
  120. data/vendor/ruby-signature/lib/ruby/signature/constant_table.rb +0 -152
  121. data/vendor/ruby-signature/lib/ruby/signature/definition.rb +0 -172
  122. data/vendor/ruby-signature/lib/ruby/signature/definition_builder.rb +0 -921
  123. data/vendor/ruby-signature/lib/ruby/signature/environment.rb +0 -283
  124. data/vendor/ruby-signature/lib/ruby/signature/environment_loader.rb +0 -138
  125. data/vendor/ruby-signature/lib/ruby/signature/environment_walker.rb +0 -126
  126. data/vendor/ruby-signature/lib/ruby/signature/errors.rb +0 -189
  127. data/vendor/ruby-signature/lib/ruby/signature/location.rb +0 -104
  128. data/vendor/ruby-signature/lib/ruby/signature/method_type.rb +0 -125
  129. data/vendor/ruby-signature/lib/ruby/signature/namespace.rb +0 -93
  130. data/vendor/ruby-signature/lib/ruby/signature/parser.y +0 -1343
  131. data/vendor/ruby-signature/lib/ruby/signature/prototype/rb.rb +0 -441
  132. data/vendor/ruby-signature/lib/ruby/signature/prototype/rbi.rb +0 -579
  133. data/vendor/ruby-signature/lib/ruby/signature/prototype/runtime.rb +0 -383
  134. data/vendor/ruby-signature/lib/ruby/signature/substitution.rb +0 -48
  135. data/vendor/ruby-signature/lib/ruby/signature/test.rb +0 -28
  136. data/vendor/ruby-signature/lib/ruby/signature/test/errors.rb +0 -63
  137. data/vendor/ruby-signature/lib/ruby/signature/test/hook.rb +0 -290
  138. data/vendor/ruby-signature/lib/ruby/signature/test/setup.rb +0 -58
  139. data/vendor/ruby-signature/lib/ruby/signature/test/spy.rb +0 -324
  140. data/vendor/ruby-signature/lib/ruby/signature/test/test_helper.rb +0 -185
  141. data/vendor/ruby-signature/lib/ruby/signature/test/type_check.rb +0 -256
  142. data/vendor/ruby-signature/lib/ruby/signature/type_name.rb +0 -72
  143. data/vendor/ruby-signature/lib/ruby/signature/types.rb +0 -932
  144. data/vendor/ruby-signature/lib/ruby/signature/variance_calculator.rb +0 -140
  145. data/vendor/ruby-signature/lib/ruby/signature/vendorer.rb +0 -49
  146. data/vendor/ruby-signature/lib/ruby/signature/version.rb +0 -5
  147. data/vendor/ruby-signature/lib/ruby/signature/writer.rb +0 -271
  148. data/vendor/ruby-signature/ruby-signature.gemspec +0 -45
  149. data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +0 -3
  150. data/vendor/ruby-signature/stdlib/base64/base64.rbs +0 -15
  151. data/vendor/ruby-signature/stdlib/builtin/array.rbs +0 -1997
  152. data/vendor/ruby-signature/stdlib/builtin/basic_object.rbs +0 -280
  153. data/vendor/ruby-signature/stdlib/builtin/binding.rbs +0 -177
  154. data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +0 -35
  155. data/vendor/ruby-signature/stdlib/builtin/class.rbs +0 -145
  156. data/vendor/ruby-signature/stdlib/builtin/comparable.rbs +0 -116
  157. data/vendor/ruby-signature/stdlib/builtin/complex.rbs +0 -400
  158. data/vendor/ruby-signature/stdlib/builtin/constants.rbs +0 -37
  159. data/vendor/ruby-signature/stdlib/builtin/data.rbs +0 -5
  160. data/vendor/ruby-signature/stdlib/builtin/deprecated.rbs +0 -2
  161. data/vendor/ruby-signature/stdlib/builtin/dir.rbs +0 -419
  162. data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +0 -606
  163. data/vendor/ruby-signature/stdlib/builtin/enumerable.rbs +0 -404
  164. data/vendor/ruby-signature/stdlib/builtin/enumerator.rbs +0 -260
  165. data/vendor/ruby-signature/stdlib/builtin/errno.rbs +0 -781
  166. data/vendor/ruby-signature/stdlib/builtin/errors.rbs +0 -582
  167. data/vendor/ruby-signature/stdlib/builtin/exception.rbs +0 -193
  168. data/vendor/ruby-signature/stdlib/builtin/false_class.rbs +0 -40
  169. data/vendor/ruby-signature/stdlib/builtin/fiber.rbs +0 -68
  170. data/vendor/ruby-signature/stdlib/builtin/fiber_error.rbs +0 -12
  171. data/vendor/ruby-signature/stdlib/builtin/file.rbs +0 -476
  172. data/vendor/ruby-signature/stdlib/builtin/file_test.rbs +0 -59
  173. data/vendor/ruby-signature/stdlib/builtin/float.rbs +0 -696
  174. data/vendor/ruby-signature/stdlib/builtin/gc.rbs +0 -121
  175. data/vendor/ruby-signature/stdlib/builtin/hash.rbs +0 -1029
  176. data/vendor/ruby-signature/stdlib/builtin/integer.rbs +0 -710
  177. data/vendor/ruby-signature/stdlib/builtin/io.rbs +0 -683
  178. data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +0 -574
  179. data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +0 -135
  180. data/vendor/ruby-signature/stdlib/builtin/match_data.rbs +0 -141
  181. data/vendor/ruby-signature/stdlib/builtin/math.rbs +0 -66
  182. data/vendor/ruby-signature/stdlib/builtin/method.rbs +0 -182
  183. data/vendor/ruby-signature/stdlib/builtin/module.rbs +0 -248
  184. data/vendor/ruby-signature/stdlib/builtin/nil_class.rbs +0 -82
  185. data/vendor/ruby-signature/stdlib/builtin/numeric.rbs +0 -409
  186. data/vendor/ruby-signature/stdlib/builtin/object.rbs +0 -824
  187. data/vendor/ruby-signature/stdlib/builtin/proc.rbs +0 -426
  188. data/vendor/ruby-signature/stdlib/builtin/process.rbs +0 -354
  189. data/vendor/ruby-signature/stdlib/builtin/random.rbs +0 -93
  190. data/vendor/ruby-signature/stdlib/builtin/range.rbs +0 -226
  191. data/vendor/ruby-signature/stdlib/builtin/rational.rbs +0 -424
  192. data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +0 -10
  193. data/vendor/ruby-signature/stdlib/builtin/regexp.rbs +0 -131
  194. data/vendor/ruby-signature/stdlib/builtin/ruby_vm.rbs +0 -14
  195. data/vendor/ruby-signature/stdlib/builtin/signal.rbs +0 -55
  196. data/vendor/ruby-signature/stdlib/builtin/string.rbs +0 -770
  197. data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +0 -13
  198. data/vendor/ruby-signature/stdlib/builtin/struct.rbs +0 -40
  199. data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +0 -230
  200. data/vendor/ruby-signature/stdlib/builtin/thread.rbs +0 -1112
  201. data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +0 -23
  202. data/vendor/ruby-signature/stdlib/builtin/time.rbs +0 -739
  203. data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +0 -91
  204. data/vendor/ruby-signature/stdlib/builtin/true_class.rbs +0 -46
  205. data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +0 -159
  206. data/vendor/ruby-signature/stdlib/builtin/warning.rbs +0 -17
  207. data/vendor/ruby-signature/stdlib/erb/erb.rbs +0 -18
  208. data/vendor/ruby-signature/stdlib/find/find.rbs +0 -44
  209. data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +0 -21
  210. data/vendor/ruby-signature/stdlib/prime/integer-extension.rbs +0 -23
  211. data/vendor/ruby-signature/stdlib/prime/prime.rbs +0 -188
  212. data/vendor/ruby-signature/stdlib/securerandom/securerandom.rbs +0 -9
  213. data/vendor/ruby-signature/stdlib/set/set.rbs +0 -77
  214. data/vendor/ruby-signature/stdlib/tmpdir/tmpdir.rbs +0 -53
@@ -1,45 +0,0 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "ruby/signature/version"
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "ruby-signature"
8
- spec.version = Ruby::Signature::VERSION
9
- spec.authors = ["Soutaro Matsumoto"]
10
- spec.email = ["matsumoto@soutaro.com"]
11
-
12
- spec.summary = %q{Type signature for Ruby classes.}
13
- spec.description = %q{Type signature for Ruby classes.}
14
- spec.homepage = "https://github.com/ruby/ruby-signature"
15
- spec.licenses = ['BSD-2-Clause', 'Ruby']
16
-
17
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
- # to allow pushing to a single host or delete this section to allow pushing to any host.
19
- if spec.respond_to?(:metadata)
20
- spec.metadata["homepage_uri"] = spec.homepage
21
- spec.metadata["source_code_uri"] = "https://github.com/ruby/ruby-signature.git"
22
- # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
23
- else
24
- raise "RubyGems 2.0 or newer is required to protect against " \
25
- "public gem pushes."
26
- end
27
-
28
- # Specify which files should be added to the gem when it is released.
29
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
- end
33
- spec.files << "lib/ruby/signature/parser.rb"
34
- spec.bindir = "exe"
35
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
- spec.require_paths = ["lib"]
37
-
38
- spec.add_development_dependency "bundler"
39
- spec.add_development_dependency "rake", "~> 13.0"
40
- spec.add_development_dependency "minitest", "~> 5.0"
41
- spec.add_development_dependency "racc", "~> 1.4.16"
42
- spec.add_development_dependency "rubocop"
43
- spec.add_development_dependency "rubocop-rubycw"
44
- spec.add_development_dependency "minitest-reporters", "~> 1.3.6"
45
- end
@@ -1,3 +0,0 @@
1
- module Abbrev
2
- def self?.abbrev: (Array[String], ?(String | Regexp | nil)) -> Hash[String, String]
3
- end
@@ -1,15 +0,0 @@
1
- module Base64
2
- def self.decode64: (String str) -> String
3
- def self.encode64: (String bin) -> String
4
- def self.strict_decode64: (String str) -> String
5
- def self.strict_encode64: (String bin) -> String
6
- def self.urlsafe_decode64: (String str) -> String
7
- def self.urlsafe_encode64: (String bin) -> String
8
-
9
- def decode64: (String str) -> String
10
- def encode64: (String bin) -> String
11
- def strict_decode64: (String str) -> String
12
- def strict_encode64: (String bin) -> String
13
- def urlsafe_decode64: (String str) -> String
14
- def urlsafe_encode64: (String bin, ?padding: bool) -> String
15
- end
@@ -1,1997 +0,0 @@
1
- # Arrays are ordered, integer-indexed collections of any object.
2
- #
3
- # Array indexing starts at 0, as in C or Java. A negative index is assumed to
4
- # be relative to the end of the array---that is, an index of -1 indicates the
5
- # last element of the array, -2 is the next to last element in the array, and so
6
- # on.
7
- #
8
- # ## Creating Arrays
9
- #
10
- # A new array can be created by using the literal constructor `[]`. Arrays can
11
- # contain different types of objects. For example, the array below contains an
12
- # Integer, a String and a Float:
13
- #
14
- # ary = [1, "two", 3.0] #=> [1, "two", 3.0]
15
- #
16
- # An array can also be created by explicitly calling Array.new with zero, one
17
- # (the initial size of the Array) or two arguments (the initial size and a
18
- # default object).
19
- #
20
- # ary = Array.new #=> []
21
- # Array.new(3) #=> [nil, nil, nil]
22
- # Array.new(3, true) #=> [true, true, true]
23
- #
24
- # Note that the second argument populates the array with references to the same
25
- # object. Therefore, it is only recommended in cases when you need to
26
- # instantiate arrays with natively immutable objects such as Symbols, numbers,
27
- # true or false.
28
- #
29
- # To create an array with separate objects a block can be passed instead. This
30
- # method is safe to use with mutable objects such as hashes, strings or other
31
- # arrays:
32
- #
33
- # Array.new(4) {Hash.new} #=> [{}, {}, {}, {}]
34
- # Array.new(4) {|i| i.to_s } #=> ["0", "1", "2", "3"]
35
- #
36
- # This is also a quick way to build up multi-dimensional arrays:
37
- #
38
- # empty_table = Array.new(3) {Array.new(3)}
39
- # #=> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
40
- #
41
- # An array can also be created by using the Array() method, provided by Kernel,
42
- # which tries to call #to_ary, then #to_a on its argument.
43
- #
44
- # Array({:a => "a", :b => "b"}) #=> [[:a, "a"], [:b, "b"]]
45
- #
46
- # ## Example Usage
47
- #
48
- # In addition to the methods it mixes in through the Enumerable module, the
49
- # Array class has proprietary methods for accessing, searching and otherwise
50
- # manipulating arrays.
51
- #
52
- # Some of the more common ones are illustrated below.
53
- #
54
- # ## Accessing Elements
55
- #
56
- # Elements in an array can be retrieved using the Array#[] method. It can take
57
- # a single integer argument (a numeric index), a pair of arguments (start and
58
- # length) or a range. Negative indices start counting from the end, with -1
59
- # being the last element.
60
- #
61
- # arr = [1, 2, 3, 4, 5, 6]
62
- # arr[2] #=> 3
63
- # arr[100] #=> nil
64
- # arr[-3] #=> 4
65
- # arr[2, 3] #=> [3, 4, 5]
66
- # arr[1..4] #=> [2, 3, 4, 5]
67
- # arr[1..-3] #=> [2, 3, 4]
68
- #
69
- # Another way to access a particular array element is by using the #at method
70
- #
71
- # arr.at(0) #=> 1
72
- #
73
- # The #slice method works in an identical manner to Array#[].
74
- #
75
- # To raise an error for indices outside of the array bounds or else to provide a
76
- # default value when that happens, you can use #fetch.
77
- #
78
- # arr = ['a', 'b', 'c', 'd', 'e', 'f']
79
- # arr.fetch(100) #=> IndexError: index 100 outside of array bounds: -6...6
80
- # arr.fetch(100, "oops") #=> "oops"
81
- #
82
- # The special methods #first and #last will return the first and last elements
83
- # of an array, respectively.
84
- #
85
- # arr.first #=> 1
86
- # arr.last #=> 6
87
- #
88
- # To return the first `n` elements of an array, use #take
89
- #
90
- # arr.take(3) #=> [1, 2, 3]
91
- #
92
- # #drop does the opposite of #take, by returning the elements after `n` elements
93
- # have been dropped:
94
- #
95
- # arr.drop(3) #=> [4, 5, 6]
96
- #
97
- # ## Obtaining Information about an Array
98
- #
99
- # Arrays keep track of their own length at all times. To query an array about
100
- # the number of elements it contains, use #length, #count or #size.
101
- #
102
- # browsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
103
- # browsers.length #=> 5
104
- # browsers.count #=> 5
105
- #
106
- # To check whether an array contains any elements at all
107
- #
108
- # browsers.empty? #=> false
109
- #
110
- # To check whether a particular item is included in the array
111
- #
112
- # browsers.include?('Konqueror') #=> false
113
- #
114
- # ## Adding Items to Arrays
115
- #
116
- # Items can be added to the end of an array by using either #push or #<<
117
- #
118
- # arr = [1, 2, 3, 4]
119
- # arr.push(5) #=> [1, 2, 3, 4, 5]
120
- # arr << 6 #=> [1, 2, 3, 4, 5, 6]
121
- #
122
- # #unshift will add a new item to the beginning of an array.
123
- #
124
- # arr.unshift(0) #=> [0, 1, 2, 3, 4, 5, 6]
125
- #
126
- # With #insert you can add a new element to an array at any position.
127
- #
128
- # arr.insert(3, 'apple') #=> [0, 1, 2, 'apple', 3, 4, 5, 6]
129
- #
130
- # Using the #insert method, you can also insert multiple values at once:
131
- #
132
- # arr.insert(3, 'orange', 'pear', 'grapefruit')
133
- # #=> [0, 1, 2, "orange", "pear", "grapefruit", "apple", 3, 4, 5, 6]
134
- #
135
- # ## Removing Items from an Array
136
- #
137
- # The method #pop removes the last element in an array and returns it:
138
- #
139
- # arr = [1, 2, 3, 4, 5, 6]
140
- # arr.pop #=> 6
141
- # arr #=> [1, 2, 3, 4, 5]
142
- #
143
- # To retrieve and at the same time remove the first item, use #shift:
144
- #
145
- # arr.shift #=> 1
146
- # arr #=> [2, 3, 4, 5]
147
- #
148
- # To delete an element at a particular index:
149
- #
150
- # arr.delete_at(2) #=> 4
151
- # arr #=> [2, 3, 5]
152
- #
153
- # To delete a particular element anywhere in an array, use #delete:
154
- #
155
- # arr = [1, 2, 2, 3]
156
- # arr.delete(2) #=> 2
157
- # arr #=> [1,3]
158
- #
159
- # A useful method if you need to remove `nil` values from an array is #compact:
160
- #
161
- # arr = ['foo', 0, nil, 'bar', 7, 'baz', nil]
162
- # arr.compact #=> ['foo', 0, 'bar', 7, 'baz']
163
- # arr #=> ['foo', 0, nil, 'bar', 7, 'baz', nil]
164
- # arr.compact! #=> ['foo', 0, 'bar', 7, 'baz']
165
- # arr #=> ['foo', 0, 'bar', 7, 'baz']
166
- #
167
- # Another common need is to remove duplicate elements from an array.
168
- #
169
- # It has the non-destructive #uniq, and destructive method #uniq!
170
- #
171
- # arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
172
- # arr.uniq #=> [2, 5, 6, 556, 8, 9, 0, 123]
173
- #
174
- # ## Iterating over Arrays
175
- #
176
- # Like all classes that include the Enumerable module, Array has an each method,
177
- # which defines what elements should be iterated over and how. In case of
178
- # Array's #each, all elements in the Array instance are yielded to the supplied
179
- # block in sequence.
180
- #
181
- # Note that this operation leaves the array unchanged.
182
- #
183
- # arr = [1, 2, 3, 4, 5]
184
- # arr.each {|a| print a -= 10, " "}
185
- # # prints: -9 -8 -7 -6 -5
186
- # #=> [1, 2, 3, 4, 5]
187
- #
188
- # Another sometimes useful iterator is #reverse_each which will iterate over the
189
- # elements in the array in reverse order.
190
- #
191
- # words = %w[first second third fourth fifth sixth]
192
- # str = ""
193
- # words.reverse_each {|word| str += "#{word} "}
194
- # p str #=> "sixth fifth fourth third second first "
195
- #
196
- # The #map method can be used to create a new array based on the original array,
197
- # but with the values modified by the supplied block:
198
- #
199
- # arr.map {|a| 2*a} #=> [2, 4, 6, 8, 10]
200
- # arr #=> [1, 2, 3, 4, 5]
201
- # arr.map! {|a| a**2} #=> [1, 4, 9, 16, 25]
202
- # arr #=> [1, 4, 9, 16, 25]
203
- #
204
- # ## Selecting Items from an Array
205
- #
206
- # Elements can be selected from an array according to criteria defined in a
207
- # block. The selection can happen in a destructive or a non-destructive manner.
208
- # While the destructive operations will modify the array they were called on,
209
- # the non-destructive methods usually return a new array with the selected
210
- # elements, but leave the original array unchanged.
211
- #
212
- # ### Non-destructive Selection
213
- #
214
- # arr = [1, 2, 3, 4, 5, 6]
215
- # arr.select {|a| a > 3} #=> [4, 5, 6]
216
- # arr.reject {|a| a < 3} #=> [3, 4, 5, 6]
217
- # arr.drop_while {|a| a < 4} #=> [4, 5, 6]
218
- # arr #=> [1, 2, 3, 4, 5, 6]
219
- #
220
- # ### Destructive Selection
221
- #
222
- # #select! and #reject! are the corresponding destructive methods to #select and
223
- # #reject
224
- #
225
- # Similar to #select vs. #reject, #delete_if and #keep_if have the exact
226
- # opposite result when supplied with the same block:
227
- #
228
- # arr.delete_if {|a| a < 4} #=> [4, 5, 6]
229
- # arr #=> [4, 5, 6]
230
- #
231
- # arr = [1, 2, 3, 4, 5, 6]
232
- # arr.keep_if {|a| a < 4} #=> [1, 2, 3]
233
- # arr #=> [1, 2, 3]
234
- # for pack.c
235
- #
236
- class Array[unchecked out Elem] < Object
237
- include Enumerable[Elem, Array[Elem]]
238
-
239
- # Returns a new array.
240
- #
241
- # In the first form, if no arguments are sent, the new array will be empty. When
242
- # a `size` and an optional `default` are sent, an array is created with `size`
243
- # copies of `default`. Take notice that all elements will reference the same
244
- # object `default`.
245
- #
246
- # The second form creates a copy of the array passed as a parameter (the array
247
- # is generated by calling to_ary on the parameter).
248
- #
249
- # first_array = ["Matz", "Guido"]
250
- #
251
- # second_array = Array.new(first_array) #=> ["Matz", "Guido"]
252
- #
253
- # first_array.equal? second_array #=> false
254
- #
255
- # In the last form, an array of the given size is created. Each element in this
256
- # array is created by passing the element's index to the given block and storing
257
- # the return value.
258
- #
259
- # Array.new(3) {|index| index ** 2}
260
- # # => [0, 1, 4]
261
- #
262
- # ## Common gotchas
263
- #
264
- # When sending the second parameter, the same object will be used as the value
265
- # for all the array elements:
266
- #
267
- # a = Array.new(2, Hash.new)
268
- # # => [{}, {}]
269
- #
270
- # a[0]['cat'] = 'feline'
271
- # a # => [{"cat"=>"feline"}, {"cat"=>"feline"}]
272
- #
273
- # a[1]['cat'] = 'Felix'
274
- # a # => [{"cat"=>"Felix"}, {"cat"=>"Felix"}]
275
- #
276
- # Since all the Array elements store the same hash, changes to one of them will
277
- # affect them all.
278
- #
279
- # If multiple copies are what you want, you should use the block version which
280
- # uses the result of that block each time an element of the array needs to be
281
- # initialized:
282
- #
283
- # a = Array.new(2) {Hash.new}
284
- # a[0]['cat'] = 'feline'
285
- # a # => [{"cat"=>"feline"}, {}]
286
- #
287
- def initialize: () -> void
288
- | (Array[Elem] ary) -> void
289
- | (int size, ?Elem val) -> void
290
- | (int size) { (Integer index) -> Elem } -> void
291
-
292
- # Returns a new array populated with the given objects.
293
- #
294
- # Array.[]( 1, 'a', /^A/) # => [1, "a", /^A/]
295
- # Array[ 1, 'a', /^A/ ] # => [1, "a", /^A/]
296
- # [ 1, 'a', /^A/ ] # => [1, "a", /^A/]
297
- #
298
- def self.[]: [U] (*U) -> ::Array[U]
299
-
300
- # Tries to convert `obj` into an array, using the `to_ary` method. Returns the
301
- # converted array or `nil` if `obj` cannot be converted. This method can be used
302
- # to check if an argument is an array.
303
- #
304
- # Array.try_convert([1]) #=> [1]
305
- # Array.try_convert("1") #=> nil
306
- #
307
- # if tmp = Array.try_convert(arg)
308
- # # the argument is an array
309
- # elsif tmp = String.try_convert(arg)
310
- # # the argument is a string
311
- # end
312
- #
313
- def self.try_convert: [U] (untyped) -> ::Array[U]?
314
-
315
- public
316
-
317
- # Set Intersection --- Returns a new array containing unique elements common to
318
- # the two arrays. The order is preserved from the original array.
319
- #
320
- # It compares elements using their #hash and #eql? methods for efficiency.
321
- #
322
- # [ 1, 1, 3, 5 ] & [ 3, 2, 1 ] #=> [ 1, 3 ]
323
- # [ 'a', 'b', 'b', 'z' ] & [ 'a', 'b', 'c' ] #=> [ 'a', 'b' ]
324
- #
325
- # See also Array#uniq.
326
- #
327
- def &: (Array[untyped] | _ToAry[untyped]) -> ::Array[Elem]
328
-
329
- # Repetition --- With a String argument, equivalent to `ary.join(str)`.
330
- #
331
- # Otherwise, returns a new array built by concatenating the `int` copies of
332
- # `self`.
333
- #
334
- # [ 1, 2, 3 ] * 3 #=> [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ]
335
- # [ 1, 2, 3 ] * "," #=> "1,2,3"
336
- #
337
- def *: (string str) -> String
338
- | (int int) -> Array[Elem]
339
-
340
- # Concatenation --- Returns a new array built by concatenating the two arrays
341
- # together to produce a third array.
342
- #
343
- # [ 1, 2, 3 ] + [ 4, 5 ] #=> [ 1, 2, 3, 4, 5 ]
344
- # a = [ "a", "b", "c" ]
345
- # c = a + [ "d", "e", "f" ]
346
- # c #=> [ "a", "b", "c", "d", "e", "f" ]
347
- # a #=> [ "a", "b", "c" ]
348
- #
349
- # Note that
350
- # x += y
351
- #
352
- # is the same as
353
- # x = x + y
354
- #
355
- # This means that it produces a new array. As a consequence, repeated use of
356
- # `+=` on arrays can be quite inefficient.
357
- #
358
- # See also Array#concat.
359
- #
360
- def +: [U] (_ToAry[U]) -> ::Array[Elem | U]
361
-
362
- # Array Difference
363
- #
364
- # Returns a new array that is a copy of the original array, removing all
365
- # occurrences of any item that also appear in `other_ary`. The order is
366
- # preserved from the original array.
367
- #
368
- # It compares elements using their #hash and #eql? methods for efficiency.
369
- #
370
- # [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #=> [ 3, 3, 5 ]
371
- #
372
- # Note that while 1 and 2 were only present once in the array argument, and were
373
- # present twice in the receiver array, all occurrences of each Integer are
374
- # removed in the returned array.
375
- #
376
- # If you need set-like behavior, see the library class Set.
377
- #
378
- # See also Array#difference.
379
- #
380
- def -: (_ToAry[untyped]) -> ::Array[Elem]
381
-
382
- # Append---Pushes the given object on to the end of this array. This expression
383
- # returns the array itself, so several appends may be chained together.
384
- #
385
- # a = [ 1, 2 ]
386
- # a << "c" << "d" << [ 3, 4 ]
387
- # #=> [ 1, 2, "c", "d", [ 3, 4 ] ]
388
- # a
389
- # #=> [ 1, 2, "c", "d", [ 3, 4 ] ]
390
- #
391
- def <<: (Elem) -> ::Array[Elem]
392
-
393
- # Comparison --- Returns an integer (`-1`, `0`, or `+1`) if this array is less
394
- # than, equal to, or greater than `other_ary`.
395
- #
396
- # Each object in each array is compared (using the <=> operator).
397
- #
398
- # Arrays are compared in an "element-wise" manner; the first element of `ary` is
399
- # compared with the first one of `other_ary` using the <=> operator, then each
400
- # of the second elements, etc... As soon as the result of any such comparison is
401
- # non zero (i.e. the two corresponding elements are not equal), that result is
402
- # returned for the whole array comparison.
403
- #
404
- # If all the elements are equal, then the result is based on a comparison of the
405
- # array lengths. Thus, two arrays are "equal" according to Array#<=> if, and
406
- # only if, they have the same length and the value of each element is equal to
407
- # the value of the corresponding element in the other array.
408
- #
409
- # `nil` is returned if the `other_ary` is not an array or if the comparison of
410
- # two elements returned `nil`.
411
- #
412
- # [ "a", "a", "c" ] <=> [ "a", "b", "c" ] #=> -1
413
- # [ 1, 2, 3, 4, 5, 6 ] <=> [ 1, 2 ] #=> +1
414
- # [ 1, 2 ] <=> [ 1, :two ] #=> nil
415
- #
416
- def <=>: (untyped) -> Integer?
417
-
418
- # Equality --- Two arrays are equal if they contain the same number of elements
419
- # and if each element is equal to (according to Object#==) the corresponding
420
- # element in `other_ary`.
421
- #
422
- # [ "a", "c" ] == [ "a", "c", 7 ] #=> false
423
- # [ "a", "c", 7 ] == [ "a", "c", 7 ] #=> true
424
- # [ "a", "c", 7 ] == [ "a", "d", "f" ] #=> false
425
- #
426
- def ==: (untyped other) -> bool
427
-
428
- # Element Reference --- Returns the element at `index`, or returns a subarray
429
- # starting at the `start` index and continuing for `length` elements, or returns
430
- # a subarray specified by `range` of indices.
431
- #
432
- # Negative indices count backward from the end of the array (-1 is the last
433
- # element). For `start` and `range` cases the starting index is just before an
434
- # element. Additionally, an empty array is returned when the starting index for
435
- # an element range is at the end of the array.
436
- #
437
- # Returns `nil` if the index (or starting index) are out of range.
438
- #
439
- # a = [ "a", "b", "c", "d", "e" ]
440
- # a[2] + a[0] + a[1] #=> "cab"
441
- # a[6] #=> nil
442
- # a[1, 2] #=> [ "b", "c" ]
443
- # a[1..3] #=> [ "b", "c", "d" ]
444
- # a[4..7] #=> [ "e" ]
445
- # a[6..10] #=> nil
446
- # a[-3, 3] #=> [ "c", "d", "e" ]
447
- # # special cases
448
- # a[5] #=> nil
449
- # a[6, 1] #=> nil
450
- # a[5, 1] #=> []
451
- # a[5..10] #=> []
452
- #
453
- def []: (int index) -> Elem
454
- | (int start, int length) -> ::Array[Elem]?
455
- | (Range[Integer] range) -> ::Array[Elem]?
456
-
457
- # Element Assignment --- Sets the element at `index`, or replaces a subarray
458
- # from the `start` index for `length` elements, or replaces a subarray specified
459
- # by the `range` of indices.
460
- #
461
- # If indices are greater than the current capacity of the array, the array grows
462
- # automatically. Elements are inserted into the array at `start` if `length` is
463
- # zero.
464
- #
465
- # Negative indices will count backward from the end of the array. For `start`
466
- # and `range` cases the starting index is just before an element.
467
- #
468
- # An IndexError is raised if a negative index points past the beginning of the
469
- # array.
470
- #
471
- # See also Array#push, and Array#unshift.
472
- #
473
- # a = Array.new
474
- # a[4] = "4"; #=> [nil, nil, nil, nil, "4"]
475
- # a[0, 3] = [ 'a', 'b', 'c' ] #=> ["a", "b", "c", nil, "4"]
476
- # a[1..2] = [ 1, 2 ] #=> ["a", 1, 2, nil, "4"]
477
- # a[0, 2] = "?" #=> ["?", 2, nil, "4"]
478
- # a[0..2] = "A" #=> ["A", "4"]
479
- # a[-1] = "Z" #=> ["A", "Z"]
480
- # a[1..-1] = nil #=> ["A", nil]
481
- # a[1..-1] = [] #=> ["A"]
482
- # a[0, 0] = [ 1, 2 ] #=> [1, 2, "A"]
483
- # a[3, 0] = "B" #=> [1, 2, "A", "B"]
484
- #
485
- def []=: (int index, Elem obj) -> Elem
486
- | (int start, int length, Elem obj) -> Elem
487
- | (int start, int length, Array[Elem]) -> Array[Elem]
488
- | (int start, int length, nil) -> nil
489
- | (Range[Integer], Elem obj) -> Elem
490
- | (Range[Integer], Array[Elem]) -> Array[Elem]
491
- | (Range[Integer], nil) -> nil
492
-
493
- # See also Enumerable#all?
494
- #
495
- def all?: () -> bool
496
- | (_Pattern[Elem] pattern) -> bool
497
- | () { (Elem obj) -> bool } -> bool
498
-
499
- # See also Enumerable#any?
500
- #
501
- alias any? all?
502
-
503
- alias append push
504
-
505
- # Searches through an array whose elements are also arrays comparing `obj` with
506
- # the first element of each contained array using `obj.==`.
507
- #
508
- # Returns the first contained array that matches (that is, the first associated
509
- # array), or `nil` if no match is found.
510
- #
511
- # See also Array#rassoc
512
- #
513
- # s1 = [ "colors", "red", "blue", "green" ]
514
- # s2 = [ "letters", "a", "b", "c" ]
515
- # s3 = "foo"
516
- # a = [ s1, s2, s3 ]
517
- # a.assoc("letters") #=> [ "letters", "a", "b", "c" ]
518
- # a.assoc("foo") #=> nil
519
- #
520
- def assoc: (untyped) -> Array[untyped]?
521
-
522
- # Returns the element at `index`. A negative index counts from the end of
523
- # `self`. Returns `nil` if the index is out of range. See also Array#[].
524
- #
525
- # a = [ "a", "b", "c", "d", "e" ]
526
- # a.at(0) #=> "a"
527
- # a.at(-1) #=> "e"
528
- #
529
- def at: (int index) -> Elem?
530
-
531
- # By using binary search, finds a value from this array which meets the given
532
- # condition in O(log n) where n is the size of the array.
533
- #
534
- # You can use this method in two modes: a find-minimum mode and a find-any mode.
535
- # In either case, the elements of the array must be monotone (or sorted) with
536
- # respect to the block.
537
- #
538
- # In find-minimum mode (this is a good choice for typical use cases), the block
539
- # must always return true or false, and there must be an index i (0 <= i <=
540
- # ary.size) so that:
541
- #
542
- # * the block returns false for any element whose index is less than i, and
543
- # * the block returns true for any element whose index is greater than or
544
- # equal to i.
545
- #
546
- #
547
- # This method returns the i-th element. If i is equal to ary.size, it returns
548
- # nil.
549
- #
550
- # ary = [0, 4, 7, 10, 12]
551
- # ary.bsearch {|x| x >= 4 } #=> 4
552
- # ary.bsearch {|x| x >= 6 } #=> 7
553
- # ary.bsearch {|x| x >= -1 } #=> 0
554
- # ary.bsearch {|x| x >= 100 } #=> nil
555
- #
556
- # In find-any mode (this behaves like libc's bsearch(3)), the block must always
557
- # return a number, and there must be two indices i and j (0 <= i <= j <=
558
- # ary.size) so that:
559
- #
560
- # * the block returns a positive number for [ary](k) if 0 <= k < i,
561
- # * the block returns zero for [ary](k) if i <= k < j, and
562
- # * the block returns a negative number for [ary](k) if j <= k < ary.size.
563
- #
564
- #
565
- # Under this condition, this method returns any element whose index is within
566
- # i...j. If i is equal to j (i.e., there is no element that satisfies the
567
- # block), this method returns nil.
568
- #
569
- # ary = [0, 4, 7, 10, 12]
570
- # # try to find v such that 4 <= v < 8
571
- # ary.bsearch {|x| 1 - x / 4 } #=> 4 or 7
572
- # # try to find v such that 8 <= v < 10
573
- # ary.bsearch {|x| 4 - x / 2 } #=> nil
574
- #
575
- # You must not mix the two modes at a time; the block must always return either
576
- # true/false, or always return a number. It is undefined which value is
577
- # actually picked up at each iteration.
578
- #
579
- def bsearch: () { (Elem) -> (true | false) } -> Elem?
580
- | () { (Elem) -> Integer } -> Elem?
581
-
582
- # By using binary search, finds an index of a value from this array which meets
583
- # the given condition in O(log n) where n is the size of the array.
584
- #
585
- # It supports two modes, depending on the nature of the block. They are exactly
586
- # the same as in the case of the #bsearch method, with the only difference being
587
- # that this method returns the index of the element instead of the element
588
- # itself. For more details consult the documentation for #bsearch.
589
- #
590
- def bsearch_index: () { (Elem) -> (true | false) } -> Integer?
591
- | () { (Elem) -> Integer } -> Integer?
592
-
593
- # Removes all elements from `self`.
594
- #
595
- # a = [ "a", "b", "c", "d", "e" ]
596
- # a.clear #=> [ ]
597
- #
598
- def clear: () -> self
599
-
600
- # Invokes the given block once for each element of `self`.
601
- #
602
- # Creates a new array containing the values returned by the block.
603
- #
604
- # See also Enumerable#collect.
605
- #
606
- # If no block is given, an Enumerator is returned instead.
607
- #
608
- # a = [ "a", "b", "c", "d" ]
609
- # a.collect {|x| x + "!"} #=> ["a!", "b!", "c!", "d!"]
610
- # a.map.with_index {|x, i| x * i} #=> ["", "b", "cc", "ddd"]
611
- # a #=> ["a", "b", "c", "d"]
612
- #
613
- def collect: [U] () { (Elem item) -> U } -> Array[U]
614
- | () -> Enumerator[Elem, Array[untyped]]
615
-
616
- # Invokes the given block once for each element of `self`, replacing the element
617
- # with the value returned by the block.
618
- #
619
- # See also Enumerable#collect.
620
- #
621
- # If no block is given, an Enumerator is returned instead.
622
- #
623
- # a = [ "a", "b", "c", "d" ]
624
- # a.map! {|x| x + "!" }
625
- # a #=> [ "a!", "b!", "c!", "d!" ]
626
- # a.collect!.with_index {|x, i| x[0...i] }
627
- # a #=> ["", "b", "c!", "d!"]
628
- #
629
- def collect!: () { (Elem item) -> Elem } -> self # collect! is monomorphic because of RBS limitation.
630
- | () -> ::Enumerator[Elem, self]
631
-
632
- # When invoked with a block, yields all combinations of length `n` of elements
633
- # from the array and then returns the array itself.
634
- #
635
- # The implementation makes no guarantees about the order in which the
636
- # combinations are yielded.
637
- #
638
- # If no block is given, an Enumerator is returned instead.
639
- #
640
- # Examples:
641
- #
642
- # a = [1, 2, 3, 4]
643
- # a.combination(1).to_a #=> [[1],[2],[3],[4]]
644
- # a.combination(2).to_a #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
645
- # a.combination(3).to_a #=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
646
- # a.combination(4).to_a #=> [[1,2,3,4]]
647
- # a.combination(0).to_a #=> [[]] # one combination of length 0
648
- # a.combination(5).to_a #=> [] # no combinations of length 5
649
- #
650
- def combination: (int n) { (Array[Elem]) -> void } -> self
651
- | (int n) -> Enumerator[Array[Elem], self]
652
-
653
- # Returns a copy of `self` with all `nil` elements removed.
654
- #
655
- # [ "a", nil, "b", nil, "c", nil ].compact
656
- # #=> [ "a", "b", "c" ]
657
- #
658
- def compact: () -> Array[Elem]
659
-
660
- # Removes `nil` elements from the array.
661
- #
662
- # Returns `nil` if no changes were made, otherwise returns the array.
663
- #
664
- # [ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
665
- # [ "a", "b", "c" ].compact! #=> nil
666
- #
667
- def compact!: () -> Array[Elem]?
668
-
669
- # Appends the elements of `other_ary`s to `self`.
670
- #
671
- # [ "a", "b" ].concat( ["c", "d"]) #=> [ "a", "b", "c", "d" ]
672
- # [ "a" ].concat( ["b"], ["c", "d"]) #=> [ "a", "b", "c", "d" ]
673
- # [ "a" ].concat #=> [ "a" ]
674
- #
675
- # a = [ 1, 2, 3 ]
676
- # a.concat( [ 4, 5 ])
677
- # a #=> [ 1, 2, 3, 4, 5 ]
678
- #
679
- # a = [ 1, 2 ]
680
- # a.concat(a, a) #=> [1, 2, 1, 2, 1, 2]
681
- #
682
- # See also Array#+.
683
- #
684
- def concat: (*Array[Elem] arrays) -> ::Array[Elem]
685
-
686
- # Returns the number of elements.
687
- #
688
- # If an argument is given, counts the number of elements which equal `obj` using
689
- # `==`.
690
- #
691
- # If a block is given, counts the number of elements for which the block returns
692
- # a true value.
693
- #
694
- # ary = [1, 2, 4, 2]
695
- # ary.count #=> 4
696
- # ary.count(2) #=> 2
697
- # ary.count {|x| x%2 == 0} #=> 3
698
- #
699
- def count: () -> Integer
700
- | (untyped obj) -> Integer
701
- | () { (Elem) -> bool } -> Integer
702
-
703
- # Calls the given block for each element `n` times or forever if `nil` is given.
704
- #
705
- # Does nothing if a non-positive number is given or the array is empty.
706
- #
707
- # Returns `nil` if the loop has finished without getting interrupted.
708
- #
709
- # If no block is given, an Enumerator is returned instead.
710
- #
711
- # a = ["a", "b", "c"]
712
- # a.cycle {|x| puts x} # print, a, b, c, a, b, c,.. forever.
713
- # a.cycle(2) {|x| puts x} # print, a, b, c, a, b, c.
714
- #
715
- def cycle: (?int? n) { (Elem) -> void } -> nil
716
- | (?int? n) -> Enumerator[Elem, nil]
717
-
718
- def deconstruct: () -> Array[Integer]
719
-
720
- # Deletes all items from `self` that are equal to `obj`.
721
- #
722
- # Returns the last deleted item, or `nil` if no matching item is found.
723
- #
724
- # If the optional code block is given, the result of the block is returned if
725
- # the item is not found. (To remove `nil` elements and get an informative
726
- # return value, use Array#compact!)
727
- #
728
- # a = [ "a", "b", "b", "b", "c" ]
729
- # a.delete("b") #=> "b"
730
- # a #=> ["a", "c"]
731
- # a.delete("z") #=> nil
732
- # a.delete("z") {"not found"} #=> "not found"
733
- #
734
- def delete: (untyped obj) -> Elem?
735
- | [S, T] (S obj) { (S) -> T } -> (Elem | T)
736
-
737
- # Deletes the element at the specified `index`, returning that element, or `nil`
738
- # if the `index` is out of range.
739
- #
740
- # See also Array#slice!
741
- #
742
- # a = ["ant", "bat", "cat", "dog"]
743
- # a.delete_at(2) #=> "cat"
744
- # a #=> ["ant", "bat", "dog"]
745
- # a.delete_at(99) #=> nil
746
- #
747
- def delete_at: (int index) -> Elem?
748
-
749
- # Deletes every element of `self` for which block evaluates to `true`.
750
- #
751
- # The array is changed instantly every time the block is called, not after the
752
- # iteration is over.
753
- #
754
- # See also Array#reject!
755
- #
756
- # If no block is given, an Enumerator is returned instead.
757
- #
758
- # scores = [ 97, 42, 75 ]
759
- # scores.delete_if {|score| score < 80 } #=> [97]
760
- #
761
- def delete_if: () { (Elem item) -> bool } -> Array[Elem]
762
- | () -> Enumerator[Elem, self]
763
-
764
- # Array Difference
765
- #
766
- # Returns a new array that is a copy of the original array, removing all
767
- # occurrences of any item that also appear in `other_ary`. The order is
768
- # preserved from the original array.
769
- #
770
- # It compares elements using their #hash and #eql? methods for efficiency.
771
- #
772
- # [ 1, 1, 2, 2, 3, 3, 4, 5 ].difference([ 1, 2, 4 ]) #=> [ 3, 3, 5 ]
773
- #
774
- # Note that while 1 and 2 were only present once in the array argument, and were
775
- # present twice in the receiver array, all occurrences of each Integer are
776
- # removed in the returned array.
777
- #
778
- # Multiple array arguments can be supplied and all occurrences of any element in
779
- # those supplied arrays that match the receiver will be removed from the
780
- # returned array.
781
- #
782
- # [ 1, 'c', :s, 'yep' ].difference([ 1 ], [ 'a', 'c' ]) #=> [ :s, "yep" ]
783
- #
784
- # If you need set-like behavior, see the library class Set.
785
- #
786
- # See also Array#-.
787
- #
788
- def difference: (*::Array[untyped] arrays) -> Array[Elem]
789
-
790
- # Extracts the nested value specified by the sequence of *idx* objects by
791
- # calling `dig` at each step, returning `nil` if any intermediate step is `nil`.
792
- #
793
- # a = [[1, [2, 3]]]
794
- #
795
- # a.dig(0, 1, 1) #=> 3
796
- # a.dig(1, 2, 3) #=> nil
797
- # a.dig(0, 0, 0) #=> TypeError: Integer does not have #dig method
798
- # [42, {foo: :bar}].dig(1, :foo) #=> :bar
799
- #
800
- def dig: (int idx) -> Elem?
801
- | (int idx, untyped, *untyped) -> untyped
802
-
803
- # Drops first `n` elements from `ary` and returns the rest of the elements in an
804
- # array.
805
- #
806
- # If a negative number is given, raises an ArgumentError.
807
- #
808
- # See also Array#take
809
- #
810
- # a = [1, 2, 3, 4, 5, 0]
811
- # a.drop(3) #=> [4, 5, 0]
812
- #
813
- def drop: (int n) -> ::Array[Elem]
814
-
815
- # Drops elements up to, but not including, the first element for which the block
816
- # returns `nil` or `false` and returns an array containing the remaining
817
- # elements.
818
- #
819
- # If no block is given, an Enumerator is returned instead.
820
- #
821
- # See also Array#take_while
822
- #
823
- # a = [1, 2, 3, 4, 5, 0]
824
- # a.drop_while {|i| i < 3 } #=> [3, 4, 5, 0]
825
- #
826
- def drop_while: () { (Elem obj) -> bool } -> Array[Elem]
827
- | () -> ::Enumerator[Elem, Array[Elem]]
828
-
829
- # Calls the given block once for each element in `self`, passing that element as
830
- # a parameter. Returns the array itself.
831
- #
832
- # If no block is given, an Enumerator is returned.
833
- #
834
- # a = [ "a", "b", "c" ]
835
- # a.each {|x| print x, " -- " }
836
- #
837
- # produces:
838
- #
839
- # a -- b -- c --
840
- #
841
- def each: () -> ::Enumerator[Elem, Array[Elem]]
842
- | () { (Elem item) -> void } -> Array[Elem]
843
-
844
- # Same as Array#each, but passes the `index` of the element instead of the
845
- # element itself.
846
- #
847
- # An Enumerator is returned if no block is given.
848
- #
849
- # a = [ "a", "b", "c" ]
850
- # a.each_index {|x| print x, " -- " }
851
- #
852
- # produces:
853
- #
854
- # 0 -- 1 -- 2 --
855
- #
856
- def each_index: () { (Integer index) -> void } -> Array[Elem]
857
- | () -> ::Enumerator[Elem, Array[Elem]]
858
-
859
- # Returns `true` if `self` contains no elements.
860
- #
861
- # [].empty? #=> true
862
- #
863
- def empty?: () -> bool
864
-
865
- # Returns `true` if `self` and `other` are the same object, or are both arrays
866
- # with the same content (according to Object#eql?).
867
- #
868
- def eql?: (untyped other) -> bool
869
-
870
- # Tries to return the element at position `index`, but throws an IndexError
871
- # exception if the referenced `index` lies outside of the array bounds. This
872
- # error can be prevented by supplying a second argument, which will act as a
873
- # `default` value.
874
- #
875
- # Alternatively, if a block is given it will only be executed when an invalid
876
- # `index` is referenced.
877
- #
878
- # Negative values of `index` count from the end of the array.
879
- #
880
- # a = [ 11, 22, 33, 44 ]
881
- # a.fetch(1) #=> 22
882
- # a.fetch(-1) #=> 44
883
- # a.fetch(4, 'cat') #=> "cat"
884
- # a.fetch(100) {|i| puts "#{i} is out of bounds"}
885
- # #=> "100 is out of bounds"
886
- #
887
- def fetch: (int index) -> Elem
888
- | [T] (int index, T default) -> (Elem | T)
889
- | [T] (Integer index) { (Integer index) -> T } -> (Elem | T)
890
-
891
- # The first three forms set the selected elements of `self` (which may be the
892
- # entire array) to `obj`.
893
- #
894
- # A `start` of `nil` is equivalent to zero.
895
- #
896
- # A `length` of `nil` is equivalent to the length of the array.
897
- #
898
- # The last three forms fill the array with the value of the given block, which
899
- # is passed the absolute index of each element to be filled.
900
- #
901
- # Negative values of `start` count from the end of the array, where `-1` is the
902
- # last element.
903
- #
904
- # a = [ "a", "b", "c", "d" ]
905
- # a.fill("x") #=> ["x", "x", "x", "x"]
906
- # a.fill("z", 2, 2) #=> ["x", "x", "z", "z"]
907
- # a.fill("y", 0..1) #=> ["y", "y", "z", "z"]
908
- # a.fill {|i| i*i} #=> [0, 1, 4, 9]
909
- # a.fill(-2) {|i| i*i*i} #=> [0, 1, 8, 27]
910
- #
911
- def fill: (Elem obj) -> self
912
- | (Elem obj, int? start, ?int? length) -> self
913
- | (Elem obj, Range[Integer] range) -> self
914
- | (?int? start, ?int? length) { (Integer index) -> Elem } -> self
915
- | (Range[Integer] range) { (Integer index) -> Elem } -> self
916
-
917
- # Returns a new array containing all elements of `ary` for which the given
918
- # `block` returns a true value.
919
- #
920
- # If no block is given, an Enumerator is returned instead.
921
- #
922
- # [1,2,3,4,5].select {|num| num.even? } #=> [2, 4]
923
- #
924
- # a = %w[ a b c d e f ]
925
- # a.select {|v| v =~ /[aeiou]/ } #=> ["a", "e"]
926
- #
927
- # See also Enumerable#select.
928
- #
929
- # Array#filter is an alias for Array#select.
930
- #
931
- def filter: () { (Elem item) -> bool } -> Array[Elem]
932
- | () -> Enumerator[Elem, Array[Elem]]
933
-
934
- # Invokes the given block passing in successive elements from `self`, deleting
935
- # elements for which the block returns a `false` value.
936
- #
937
- # The array may not be changed instantly every time the block is called.
938
- #
939
- # If changes were made, it will return `self`, otherwise it returns `nil`.
940
- #
941
- # If no block is given, an Enumerator is returned instead.
942
- #
943
- # See also Array#keep_if.
944
- #
945
- # Array#filter! is an alias for Array#select!.
946
- #
947
- def filter!: () { (Elem item) -> bool } -> Array[Elem]?
948
- | () -> Enumerator[Elem, Array[Elem]?]
949
-
950
- # Returns the *index* of the first object in `ary` such that the object is `==`
951
- # to `obj`.
952
- #
953
- # If a block is given instead of an argument, returns the *index* of the first
954
- # object for which the block returns `true`. Returns `nil` if no match is
955
- # found.
956
- #
957
- # See also Array#rindex.
958
- #
959
- # An Enumerator is returned if neither a block nor argument is given.
960
- #
961
- # a = [ "a", "b", "c" ]
962
- # a.index("b") #=> 1
963
- # a.index("z") #=> nil
964
- # a.index {|x| x == "b"} #=> 1
965
- #
966
- def find_index: (untyped obj) -> Integer?
967
- | () { (Elem item) -> bool } -> Integer?
968
- | () -> Enumerator[Elem, Integer?]
969
-
970
- # Returns the first element, or the first `n` elements, of the array. If the
971
- # array is empty, the first form returns `nil`, and the second form returns an
972
- # empty array. See also Array#last for the opposite effect.
973
- #
974
- # a = [ "q", "r", "s", "t" ]
975
- # a.first #=> "q"
976
- # a.first(2) #=> ["q", "r"]
977
- #
978
- def first: () -> Elem?
979
- | (int n) -> Array[Elem]
980
-
981
- # Returns a new array that is a one-dimensional flattening of `self`
982
- # (recursively).
983
- #
984
- # That is, for every element that is an array, extract its elements into the new
985
- # array.
986
- #
987
- # The optional `level` argument determines the level of recursion to flatten.
988
- #
989
- # s = [ 1, 2, 3 ] #=> [1, 2, 3]
990
- # t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
991
- # a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
992
- # a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
993
- # a = [ 1, 2, [3, [4, 5] ] ]
994
- # a.flatten(1) #=> [1, 2, 3, [4, 5]]
995
- #
996
- def flatten: (?int level) -> ::Array[untyped]
997
-
998
- # Flattens `self` in place.
999
- #
1000
- # Returns `nil` if no modifications were made (i.e., the array contains no
1001
- # subarrays.)
1002
- #
1003
- # The optional `level` argument determines the level of recursion to flatten.
1004
- #
1005
- # a = [ 1, 2, [3, [4, 5] ] ]
1006
- # a.flatten! #=> [1, 2, 3, 4, 5]
1007
- # a.flatten! #=> nil
1008
- # a #=> [1, 2, 3, 4, 5]
1009
- # a = [ 1, 2, [3, [4, 5] ] ]
1010
- # a.flatten!(1) #=> [1, 2, 3, [4, 5]]
1011
- #
1012
- def flatten!: (?int level) -> Array[untyped]?
1013
-
1014
- # Compute a hash-code for this array.
1015
- #
1016
- # Two arrays with the same content will have the same hash code (and will
1017
- # compare using #eql?).
1018
- #
1019
- # See also Object#hash.
1020
- #
1021
- def hash: () -> Integer
1022
-
1023
- # Returns `true` if the given `object` is present in `self` (that is, if any
1024
- # element `==` `object`), otherwise returns `false`.
1025
- #
1026
- # a = [ "a", "b", "c" ]
1027
- # a.include?("b") #=> true
1028
- # a.include?("z") #=> false
1029
- #
1030
- def include?: (untyped object) -> bool
1031
-
1032
- # Returns the *index* of the first object in `ary` such that the object is `==`
1033
- # to `obj`.
1034
- #
1035
- # If a block is given instead of an argument, returns the *index* of the first
1036
- # object for which the block returns `true`. Returns `nil` if no match is
1037
- # found.
1038
- #
1039
- # See also Array#rindex.
1040
- #
1041
- # An Enumerator is returned if neither a block nor argument is given.
1042
- #
1043
- # a = [ "a", "b", "c" ]
1044
- # a.index("b") #=> 1
1045
- # a.index("z") #=> nil
1046
- # a.index {|x| x == "b"} #=> 1
1047
- #
1048
- alias index find_index
1049
-
1050
- # Inserts the given values before the element with the given `index`.
1051
- #
1052
- # Negative indices count backwards from the end of the array, where `-1` is the
1053
- # last element. If a negative index is used, the given values will be inserted
1054
- # after that element, so using an index of `-1` will insert the values at the
1055
- # end of the array.
1056
- #
1057
- # a = %w{ a b c d }
1058
- # a.insert(2, 99) #=> ["a", "b", 99, "c", "d"]
1059
- # a.insert(-2, 1, 2, 3) #=> ["a", "b", 99, "c", 1, 2, 3, "d"]
1060
- #
1061
- def insert: (int index, *Elem obj) -> Array[Elem]
1062
-
1063
- # Creates a string representation of `self`, by calling #inspect on each
1064
- # element.
1065
- #
1066
- # [ "a", "b", "c" ].to_s #=> "[\"a\", \"b\", \"c\"]"
1067
- #
1068
- def inspect: () -> String
1069
-
1070
- # Set Intersection --- Returns a new array containing unique elements common to
1071
- # `self` and `other_ary`s. Order is preserved from the original array.
1072
- #
1073
- # It compares elements using their #hash and #eql? methods for efficiency.
1074
- #
1075
- # [ 1, 1, 3, 5 ].intersection([ 3, 2, 1 ]) # => [ 1, 3 ]
1076
- # [ "a", "b", "z" ].intersection([ "a", "b", "c" ], [ "b" ]) # => [ "b" ]
1077
- # [ "a" ].intersection #=> [ "a" ]
1078
- #
1079
- # See also Array#&.
1080
- #
1081
- def intersection: (*Array[untyped] | _ToAry[untyped] other_ary) -> Array[Elem]
1082
-
1083
- # Returns a string created by converting each element of the array to a string,
1084
- # separated by the given `separator`. If the `separator` is `nil`, it uses
1085
- # current `$,`. If both the `separator` and `$,` are `nil`, it uses an empty
1086
- # string.
1087
- #
1088
- # [ "a", "b", "c" ].join #=> "abc"
1089
- # [ "a", "b", "c" ].join("-") #=> "a-b-c"
1090
- #
1091
- # For nested arrays, join is applied recursively:
1092
- #
1093
- # [ "a", [1, 2, [:x, :y]], "b" ].join("-") #=> "a-1-2-x-y-b"
1094
- #
1095
- def join: (?string separator) -> String
1096
-
1097
- # Deletes every element of `self` for which the given block evaluates to
1098
- # `false`, and returns `self`.
1099
- #
1100
- # If no block is given, an Enumerator is returned instead.
1101
- #
1102
- # a = %w[ a b c d e f ]
1103
- # a.keep_if {|v| v =~ /[aeiou]/ } #=> ["a", "e"]
1104
- # a #=> ["a", "e"]
1105
- #
1106
- # See also Array#select!.
1107
- #
1108
- def keep_if: () { (Elem item) -> bool } -> Array[Elem]
1109
- | () -> Enumerator[Elem, Array[Elem]]
1110
-
1111
- # Returns the last element(s) of `self`. If the array is empty, the first form
1112
- # returns `nil`.
1113
- #
1114
- # See also Array#first for the opposite effect.
1115
- #
1116
- # a = [ "w", "x", "y", "z" ]
1117
- # a.last #=> "z"
1118
- # a.last(2) #=> ["y", "z"]
1119
- #
1120
- def last: () -> Elem?
1121
- | (int n) -> Array[Elem]
1122
-
1123
- # Returns the number of elements in `self`. May be zero.
1124
- #
1125
- # [ 1, 2, 3, 4, 5 ].length #=> 5
1126
- # [].length #=> 0
1127
- #
1128
- def length: () -> Integer
1129
-
1130
- # Invokes the given block once for each element of `self`.
1131
- #
1132
- # Creates a new array containing the values returned by the block.
1133
- #
1134
- # See also Enumerable#collect.
1135
- #
1136
- # If no block is given, an Enumerator is returned instead.
1137
- #
1138
- # a = [ "a", "b", "c", "d" ]
1139
- # a.collect {|x| x + "!"} #=> ["a!", "b!", "c!", "d!"]
1140
- # a.map.with_index {|x, i| x * i} #=> ["", "b", "cc", "ddd"]
1141
- # a #=> ["a", "b", "c", "d"]
1142
- #
1143
- alias map collect
1144
-
1145
- # Invokes the given block once for each element of `self`, replacing the element
1146
- # with the value returned by the block.
1147
- #
1148
- # See also Enumerable#collect.
1149
- #
1150
- # If no block is given, an Enumerator is returned instead.
1151
- #
1152
- # a = [ "a", "b", "c", "d" ]
1153
- # a.map! {|x| x + "!" }
1154
- # a #=> [ "a!", "b!", "c!", "d!" ]
1155
- # a.collect!.with_index {|x, i| x[0...i] }
1156
- # a #=> ["", "b", "c!", "d!"]
1157
- #
1158
- alias map! collect!
1159
-
1160
- # Returns the object in *ary* with the maximum value. The first form assumes all
1161
- # objects implement Comparable; the second uses the block to return *a <=> b*.
1162
- #
1163
- # ary = %w(albatross dog horse)
1164
- # ary.max #=> "horse"
1165
- # ary.max {|a, b| a.length <=> b.length} #=> "albatross"
1166
- #
1167
- # If the `n` argument is given, maximum `n` elements are returned as an array.
1168
- #
1169
- # ary = %w[albatross dog horse]
1170
- # ary.max(2) #=> ["horse", "dog"]
1171
- # ary.max(2) {|a, b| a.length <=> b.length } #=> ["albatross", "horse"]
1172
- #
1173
- def max: () -> Elem?
1174
- | () { (Elem a, Elem b) -> Integer? } -> Elem?
1175
- | (int n) -> Array[Elem]
1176
- | (int n) { (Elem a, Elem b) -> Integer? } -> Array[Elem]
1177
-
1178
- # Returns the object in *ary* with the minimum value. The first form assumes all
1179
- # objects implement Comparable; the second uses the block to return *a <=> b*.
1180
- #
1181
- # ary = %w(albatross dog horse)
1182
- # ary.min #=> "albatross"
1183
- # ary.min {|a, b| a.length <=> b.length} #=> "dog"
1184
- #
1185
- # If the `n` argument is given, minimum `n` elements are returned as an array.
1186
- #
1187
- # ary = %w[albatross dog horse]
1188
- # ary.min(2) #=> ["albatross", "dog"]
1189
- # ary.min(2) {|a, b| a.length <=> b.length } #=> ["dog", "horse"]
1190
- #
1191
- alias min max
1192
-
1193
- # Returns a two element array which contains the minimum and the maximum value
1194
- # in the array.
1195
- #
1196
- # Can be given an optional block to override the default comparison method `a
1197
- # <=> b`.
1198
- #
1199
- def minmax: () -> [ Elem?, Elem? ]
1200
- | () { (Elem a, Elem b) -> Integer? } -> [ Elem?, Elem? ]
1201
-
1202
- # See also Enumerable#none?
1203
- #
1204
- alias none? all?
1205
-
1206
- # See also Enumerable#one?
1207
- #
1208
- alias one? none?
1209
-
1210
- # Packs the contents of *arr* into a binary sequence according to the directives
1211
- # in *aTemplateString* (see the table below) Directives ``A,'' ``a,'' and ``Z''
1212
- # may be followed by a count, which gives the width of the resulting field. The
1213
- # remaining directives also may take a count, indicating the number of array
1214
- # elements to convert. If the count is an asterisk (```*`''), all remaining
1215
- # array elements will be converted. Any of the directives ```sSiIlL`'' may be
1216
- # followed by an underscore (```_`'') or exclamation mark (```!`'') to use the
1217
- # underlying platform's native size for the specified type; otherwise, they use
1218
- # a platform-independent size. Spaces are ignored in the template string. See
1219
- # also String#unpack.
1220
- #
1221
- # a = [ "a", "b", "c" ]
1222
- # n = [ 65, 66, 67 ]
1223
- # a.pack("A3A3A3") #=> "a b c "
1224
- # a.pack("a3a3a3") #=> "a\000\000b\000\000c\000\000"
1225
- # n.pack("ccc") #=> "ABC"
1226
- #
1227
- # If *aBufferString* is specified and its capacity is enough, `pack` uses it as
1228
- # the buffer and returns it. When the offset is specified by the beginning of
1229
- # *aTemplateString*, the result is filled after the offset. If original contents
1230
- # of *aBufferString* exists and it's longer than the offset, the rest of
1231
- # *offsetOfBuffer* are overwritten by the result. If it's shorter, the gap is
1232
- # filled with ```\0`''.
1233
- #
1234
- # Note that ``buffer:'' option does not guarantee not to allocate memory in
1235
- # `pack`. If the capacity of *aBufferString* is not enough, `pack` allocates
1236
- # memory.
1237
- #
1238
- # Directives for `pack`.
1239
- #
1240
- # Integer | Array |
1241
- # Directive | Element | Meaning
1242
- # ----------------------------------------------------------------------------
1243
- # C | Integer | 8-bit unsigned (unsigned char)
1244
- # S | Integer | 16-bit unsigned, native endian (uint16_t)
1245
- # L | Integer | 32-bit unsigned, native endian (uint32_t)
1246
- # Q | Integer | 64-bit unsigned, native endian (uint64_t)
1247
- # J | Integer | pointer width unsigned, native endian (uintptr_t)
1248
- # | | (J is available since Ruby 2.3.)
1249
- # | |
1250
- # c | Integer | 8-bit signed (signed char)
1251
- # s | Integer | 16-bit signed, native endian (int16_t)
1252
- # l | Integer | 32-bit signed, native endian (int32_t)
1253
- # q | Integer | 64-bit signed, native endian (int64_t)
1254
- # j | Integer | pointer width signed, native endian (intptr_t)
1255
- # | | (j is available since Ruby 2.3.)
1256
- # | |
1257
- # S_ S! | Integer | unsigned short, native endian
1258
- # I I_ I! | Integer | unsigned int, native endian
1259
- # L_ L! | Integer | unsigned long, native endian
1260
- # Q_ Q! | Integer | unsigned long long, native endian (ArgumentError
1261
- # | | if the platform has no long long type.)
1262
- # | | (Q_ and Q! is available since Ruby 2.1.)
1263
- # J! | Integer | uintptr_t, native endian (same with J)
1264
- # | | (J! is available since Ruby 2.3.)
1265
- # | |
1266
- # s_ s! | Integer | signed short, native endian
1267
- # i i_ i! | Integer | signed int, native endian
1268
- # l_ l! | Integer | signed long, native endian
1269
- # q_ q! | Integer | signed long long, native endian (ArgumentError
1270
- # | | if the platform has no long long type.)
1271
- # | | (q_ and q! is available since Ruby 2.1.)
1272
- # j! | Integer | intptr_t, native endian (same with j)
1273
- # | | (j! is available since Ruby 2.3.)
1274
- # | |
1275
- # S> s> S!> s!> | Integer | same as the directives without ">" except
1276
- # L> l> L!> l!> | | big endian
1277
- # I!> i!> | | (available since Ruby 1.9.3)
1278
- # Q> q> Q!> q!> | | "S>" is same as "n"
1279
- # J> j> J!> j!> | | "L>" is same as "N"
1280
- # | |
1281
- # S< s< S!< s!< | Integer | same as the directives without "<" except
1282
- # L< l< L!< l!< | | little endian
1283
- # I!< i!< | | (available since Ruby 1.9.3)
1284
- # Q< q< Q!< q!< | | "S<" is same as "v"
1285
- # J< j< J!< j!< | | "L<" is same as "V"
1286
- # | |
1287
- # n | Integer | 16-bit unsigned, network (big-endian) byte order
1288
- # N | Integer | 32-bit unsigned, network (big-endian) byte order
1289
- # v | Integer | 16-bit unsigned, VAX (little-endian) byte order
1290
- # V | Integer | 32-bit unsigned, VAX (little-endian) byte order
1291
- # | |
1292
- # U | Integer | UTF-8 character
1293
- # w | Integer | BER-compressed integer
1294
- #
1295
- # Float | Array |
1296
- # Directive | Element | Meaning
1297
- # ---------------------------------------------------------------------------
1298
- # D d | Float | double-precision, native format
1299
- # F f | Float | single-precision, native format
1300
- # E | Float | double-precision, little-endian byte order
1301
- # e | Float | single-precision, little-endian byte order
1302
- # G | Float | double-precision, network (big-endian) byte order
1303
- # g | Float | single-precision, network (big-endian) byte order
1304
- #
1305
- # String | Array |
1306
- # Directive | Element | Meaning
1307
- # ---------------------------------------------------------------------------
1308
- # A | String | arbitrary binary string (space padded, count is width)
1309
- # a | String | arbitrary binary string (null padded, count is width)
1310
- # Z | String | same as ``a'', except that null is added with *
1311
- # B | String | bit string (MSB first)
1312
- # b | String | bit string (LSB first)
1313
- # H | String | hex string (high nibble first)
1314
- # h | String | hex string (low nibble first)
1315
- # u | String | UU-encoded string
1316
- # M | String | quoted printable, MIME encoding (see also RFC2045)
1317
- # | | (text mode but input must use LF and output LF)
1318
- # m | String | base64 encoded string (see RFC 2045)
1319
- # | | (if count is 0, no line feed are added, see RFC 4648)
1320
- # | | (count specifies input bytes between each LF,
1321
- # | | rounded down to nearest multiple of 3)
1322
- # P | String | pointer to a structure (fixed-length string)
1323
- # p | String | pointer to a null-terminated string
1324
- #
1325
- # Misc. | Array |
1326
- # Directive | Element | Meaning
1327
- # ---------------------------------------------------------------------------
1328
- # @ | --- | moves to absolute position
1329
- # X | --- | back up a byte
1330
- # x | --- | null byte
1331
- #
1332
- def pack: (string fmt, ?buffer: String?) -> String
1333
-
1334
- # When invoked with a block, yield all permutations of length `n` of the
1335
- # elements of the array, then return the array itself.
1336
- #
1337
- # If `n` is not specified, yield all permutations of all elements.
1338
- #
1339
- # The implementation makes no guarantees about the order in which the
1340
- # permutations are yielded.
1341
- #
1342
- # If no block is given, an Enumerator is returned instead.
1343
- #
1344
- # Examples:
1345
- #
1346
- # a = [1, 2, 3]
1347
- # a.permutation.to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
1348
- # a.permutation(1).to_a #=> [[1],[2],[3]]
1349
- # a.permutation(2).to_a #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
1350
- # a.permutation(3).to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
1351
- # a.permutation(0).to_a #=> [[]] # one permutation of length 0
1352
- # a.permutation(4).to_a #=> [] # no permutations of length 4
1353
- #
1354
- def permutation: (?Integer n) -> Enumerator[Array[Elem], Array[Elem]]
1355
- | (?Integer n) { (Array[Elem] p) -> void } -> Array[Elem]
1356
-
1357
- # Removes the last element from `self` and returns it, or `nil` if the array is
1358
- # empty.
1359
- #
1360
- # If a number `n` is given, returns an array of the last `n` elements (or less)
1361
- # just like `array.slice!(-n, n)` does. See also Array#push for the opposite
1362
- # effect.
1363
- #
1364
- # a = [ "a", "b", "c", "d" ]
1365
- # a.pop #=> "d"
1366
- # a.pop(2) #=> ["b", "c"]
1367
- # a #=> ["a"]
1368
- #
1369
- def pop: () -> Elem?
1370
- | (int n) -> Array[Elem]
1371
-
1372
- alias prepend unshift
1373
-
1374
- # Returns an array of all combinations of elements from all arrays.
1375
- #
1376
- # The length of the returned array is the product of the length of `self` and
1377
- # the argument arrays.
1378
- #
1379
- # If given a block, #product will yield all combinations and return `self`
1380
- # instead.
1381
- #
1382
- # [1,2,3].product([4,5]) #=> [[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]]
1383
- # [1,2].product([1,2]) #=> [[1,1],[1,2],[2,1],[2,2]]
1384
- # [1,2].product([3,4],[5,6]) #=> [[1,3,5],[1,3,6],[1,4,5],[1,4,6],
1385
- # # [2,3,5],[2,3,6],[2,4,5],[2,4,6]]
1386
- # [1,2].product() #=> [[1],[2]]
1387
- # [1,2].product([]) #=> []
1388
- #
1389
- def product: () -> Array[[Elem]]
1390
- | [X] (Array[X] other_ary) -> Array[[Elem, X]]
1391
- | [X, Y] (Array[X] other_ary1, Array[Y] other_ary2) -> Array[[Elem, X, Y]]
1392
- | [U] (*::Array[U] other_arys) -> Array[Array[Elem | U]]
1393
-
1394
- # Append --- Pushes the given object(s) on to the end of this array. This
1395
- # expression returns the array itself, so several appends may be chained
1396
- # together. See also Array#pop for the opposite effect.
1397
- #
1398
- # a = [ "a", "b", "c" ]
1399
- # a.push("d", "e", "f")
1400
- # #=> ["a", "b", "c", "d", "e", "f"]
1401
- # [1, 2, 3].push(4).push(5)
1402
- # #=> [1, 2, 3, 4, 5]
1403
- #
1404
- def push: (*Elem obj) -> Array[Elem]
1405
-
1406
- # Searches through the array whose elements are also arrays.
1407
- #
1408
- # Compares `obj` with the second element of each contained array using `obj.==`.
1409
- #
1410
- # Returns the first contained array that matches `obj`.
1411
- #
1412
- # See also Array#assoc.
1413
- #
1414
- # a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]
1415
- # a.rassoc("two") #=> [2, "two"]
1416
- # a.rassoc("four") #=> nil
1417
- #
1418
- alias rassoc assoc
1419
-
1420
- # Returns a new array containing the items in `self` for which the given block
1421
- # is not `true`. The ordering of non-rejected elements is maintained.
1422
- #
1423
- # See also Array#delete_if
1424
- #
1425
- # If no block is given, an Enumerator is returned instead.
1426
- #
1427
- alias reject delete_if
1428
-
1429
- # Deletes every element of `self` for which the block evaluates to `true`, if no
1430
- # changes were made returns `nil`.
1431
- #
1432
- # The array may not be changed instantly every time the block is called.
1433
- #
1434
- # See also Enumerable#reject and Array#delete_if.
1435
- #
1436
- # If no block is given, an Enumerator is returned instead.
1437
- #
1438
- def reject!: () { (Elem item) -> bool } -> Array[Elem]?
1439
- | () -> ::Enumerator[Elem, Array[Elem]?]
1440
-
1441
- # When invoked with a block, yields all repeated combinations of length `n` of
1442
- # elements from the array and then returns the array itself.
1443
- #
1444
- # The implementation makes no guarantees about the order in which the repeated
1445
- # combinations are yielded.
1446
- #
1447
- # If no block is given, an Enumerator is returned instead.
1448
- #
1449
- # Examples:
1450
- #
1451
- # a = [1, 2, 3]
1452
- # a.repeated_combination(1).to_a #=> [[1], [2], [3]]
1453
- # a.repeated_combination(2).to_a #=> [[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]]
1454
- # a.repeated_combination(3).to_a #=> [[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],
1455
- # # [1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]]
1456
- # a.repeated_combination(4).to_a #=> [[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,2,2],[1,1,2,3],
1457
- # # [1,1,3,3],[1,2,2,2],[1,2,2,3],[1,2,3,3],[1,3,3,3],
1458
- # # [2,2,2,2],[2,2,2,3],[2,2,3,3],[2,3,3,3],[3,3,3,3]]
1459
- # a.repeated_combination(0).to_a #=> [[]] # one combination of length 0
1460
- #
1461
- def repeated_combination: (int n) { (Array[Elem] c) -> void } -> self
1462
- | (int n) -> Enumerator[Array[Elem], self]
1463
-
1464
- # When invoked with a block, yield all repeated permutations of length `n` of
1465
- # the elements of the array, then return the array itself.
1466
- #
1467
- # The implementation makes no guarantees about the order in which the repeated
1468
- # permutations are yielded.
1469
- #
1470
- # If no block is given, an Enumerator is returned instead.
1471
- #
1472
- # Examples:
1473
- #
1474
- # a = [1, 2]
1475
- # a.repeated_permutation(1).to_a #=> [[1], [2]]
1476
- # a.repeated_permutation(2).to_a #=> [[1,1],[1,2],[2,1],[2,2]]
1477
- # a.repeated_permutation(3).to_a #=> [[1,1,1],[1,1,2],[1,2,1],[1,2,2],
1478
- # # [2,1,1],[2,1,2],[2,2,1],[2,2,2]]
1479
- # a.repeated_permutation(0).to_a #=> [[]] # one permutation of length 0
1480
- #
1481
- def repeated_permutation: (int n) { (Array[Elem] p) -> void } -> self
1482
- | (int n) -> Enumerator[Array[Elem], self]
1483
-
1484
- # Replaces the contents of `self` with the contents of `other_ary`, truncating
1485
- # or expanding if necessary.
1486
- #
1487
- # a = [ "a", "b", "c", "d", "e" ]
1488
- # a.replace([ "x", "y", "z" ]) #=> ["x", "y", "z"]
1489
- # a #=> ["x", "y", "z"]
1490
- #
1491
- def replace: (Array[Elem]) -> self
1492
-
1493
- # Returns a new array containing `self`'s elements in reverse order.
1494
- #
1495
- # [ "a", "b", "c" ].reverse #=> ["c", "b", "a"]
1496
- # [ 1 ].reverse #=> [1]
1497
- #
1498
- def reverse: () -> Array[Elem]
1499
-
1500
- # Reverses `self` in place.
1501
- #
1502
- # a = [ "a", "b", "c" ]
1503
- # a.reverse! #=> ["c", "b", "a"]
1504
- # a #=> ["c", "b", "a"]
1505
- #
1506
- def reverse!: () -> Array[Elem]
1507
-
1508
- # Same as Array#each, but traverses `self` in reverse order.
1509
- #
1510
- # a = [ "a", "b", "c" ]
1511
- # a.reverse_each {|x| print x, " " }
1512
- #
1513
- # produces:
1514
- #
1515
- # c b a
1516
- #
1517
- def reverse_each: () { (Elem item) -> void } -> Array[Elem]
1518
- | () -> Enumerator[Elem, Array[Elem]]
1519
-
1520
- # Returns the *index* of the last object in `self` `==` to `obj`.
1521
- #
1522
- # If a block is given instead of an argument, returns the *index* of the first
1523
- # object for which the block returns `true`, starting from the last object.
1524
- #
1525
- # Returns `nil` if no match is found.
1526
- #
1527
- # See also Array#index.
1528
- #
1529
- # If neither block nor argument is given, an Enumerator is returned instead.
1530
- #
1531
- # a = [ "a", "b", "b", "b", "c" ]
1532
- # a.rindex("b") #=> 3
1533
- # a.rindex("z") #=> nil
1534
- # a.rindex {|x| x == "b"} #=> 3
1535
- #
1536
- def rindex: (untyped obj) -> Integer?
1537
- | () { (Elem item) -> bool } -> Integer?
1538
- | () -> Enumerator[Elem, Integer?]
1539
-
1540
- # Returns a new array by rotating `self` so that the element at `count` is the
1541
- # first element of the new array.
1542
- #
1543
- # If `count` is negative then it rotates in the opposite direction, starting
1544
- # from the end of `self` where `-1` is the last element.
1545
- #
1546
- # a = [ "a", "b", "c", "d" ]
1547
- # a.rotate #=> ["b", "c", "d", "a"]
1548
- # a #=> ["a", "b", "c", "d"]
1549
- # a.rotate(2) #=> ["c", "d", "a", "b"]
1550
- # a.rotate(-3) #=> ["b", "c", "d", "a"]
1551
- #
1552
- def rotate: (?int count) -> Array[Elem]
1553
-
1554
- # Rotates `self` in place so that the element at `count` comes first, and
1555
- # returns `self`.
1556
- #
1557
- # If `count` is negative then it rotates in the opposite direction, starting
1558
- # from the end of the array where `-1` is the last element.
1559
- #
1560
- # a = [ "a", "b", "c", "d" ]
1561
- # a.rotate! #=> ["b", "c", "d", "a"]
1562
- # a #=> ["b", "c", "d", "a"]
1563
- # a.rotate!(2) #=> ["d", "a", "b", "c"]
1564
- # a.rotate!(-3) #=> ["a", "b", "c", "d"]
1565
- #
1566
- def rotate!: (?int count) -> Array[Elem]
1567
-
1568
- # Choose a random element or `n` random elements from the array.
1569
- #
1570
- # The elements are chosen by using random and unique indices into the array in
1571
- # order to ensure that an element doesn't repeat itself unless the array already
1572
- # contained duplicate elements.
1573
- #
1574
- # If the array is empty the first form returns `nil` and the second form returns
1575
- # an empty array.
1576
- #
1577
- # a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
1578
- # a.sample #=> 7
1579
- # a.sample(4) #=> [6, 4, 2, 5]
1580
- #
1581
- # The optional `rng` argument will be used as the random number generator.
1582
- #
1583
- # a.sample(random: Random.new(1)) #=> 6
1584
- # a.sample(4, random: Random.new(1)) #=> [6, 10, 9, 2]
1585
- #
1586
- def sample: (?random: Random rng) -> Elem?
1587
- | (?int n, ?random: Random rng) -> ::Array[Elem]
1588
-
1589
- # Returns a new array containing all elements of `ary` for which the given
1590
- # `block` returns a true value.
1591
- #
1592
- # If no block is given, an Enumerator is returned instead.
1593
- #
1594
- # [1,2,3,4,5].select {|num| num.even? } #=> [2, 4]
1595
- #
1596
- # a = %w[ a b c d e f ]
1597
- # a.select {|v| v =~ /[aeiou]/ } #=> ["a", "e"]
1598
- #
1599
- # See also Enumerable#select.
1600
- #
1601
- # Array#filter is an alias for Array#select.
1602
- #
1603
- def select: () { (Elem item) -> bool } -> Array[Elem]
1604
- | () -> Enumerator[Elem, Array[Elem]]
1605
-
1606
- # Invokes the given block passing in successive elements from `self`, deleting
1607
- # elements for which the block returns a `false` value.
1608
- #
1609
- # The array may not be changed instantly every time the block is called.
1610
- #
1611
- # If changes were made, it will return `self`, otherwise it returns `nil`.
1612
- #
1613
- # If no block is given, an Enumerator is returned instead.
1614
- #
1615
- # See also Array#keep_if.
1616
- #
1617
- # Array#filter! is an alias for Array#select!.
1618
- #
1619
- def select!: () { (Elem item) -> bool } -> Array[Elem]?
1620
- | () -> ::Enumerator[Elem, Array[Elem]?]
1621
-
1622
- # Removes the first element of `self` and returns it (shifting all other
1623
- # elements down by one). Returns `nil` if the array is empty.
1624
- #
1625
- # If a number `n` is given, returns an array of the first `n` elements (or less)
1626
- # just like `array.slice!(0, n)` does. With `ary` containing only the remainder
1627
- # elements, not including what was shifted to `new_ary`. See also Array#unshift
1628
- # for the opposite effect.
1629
- #
1630
- # args = [ "-m", "-q", "filename" ]
1631
- # args.shift #=> "-m"
1632
- # args #=> ["-q", "filename"]
1633
- #
1634
- # args = [ "-m", "-q", "filename" ]
1635
- # args.shift(2) #=> ["-m", "-q"]
1636
- # args #=> ["filename"]
1637
- #
1638
- def shift: () -> Elem?
1639
- | (?int n) -> Array[Elem]
1640
-
1641
- # Returns a new array with elements of `self` shuffled.
1642
- #
1643
- # a = [ 1, 2, 3 ] #=> [1, 2, 3]
1644
- # a.shuffle #=> [2, 3, 1]
1645
- # a #=> [1, 2, 3]
1646
- #
1647
- # The optional `rng` argument will be used as the random number generator.
1648
- #
1649
- # a.shuffle(random: Random.new(1)) #=> [1, 3, 2]
1650
- #
1651
- def shuffle: (?random: Random rng) -> Array[Elem]
1652
-
1653
- # Shuffles elements in `self` in place.
1654
- #
1655
- # a = [ 1, 2, 3 ] #=> [1, 2, 3]
1656
- # a.shuffle! #=> [2, 3, 1]
1657
- # a #=> [2, 3, 1]
1658
- #
1659
- # The optional `rng` argument will be used as the random number generator.
1660
- #
1661
- # a.shuffle!(random: Random.new(1)) #=> [1, 3, 2]
1662
- #
1663
- def shuffle!: (?random: Random rng) -> Array[Elem]
1664
-
1665
- alias size length
1666
-
1667
- # Element Reference --- Returns the element at `index`, or returns a subarray
1668
- # starting at the `start` index and continuing for `length` elements, or returns
1669
- # a subarray specified by `range` of indices.
1670
- #
1671
- # Negative indices count backward from the end of the array (-1 is the last
1672
- # element). For `start` and `range` cases the starting index is just before an
1673
- # element. Additionally, an empty array is returned when the starting index for
1674
- # an element range is at the end of the array.
1675
- #
1676
- # Returns `nil` if the index (or starting index) are out of range.
1677
- #
1678
- # a = [ "a", "b", "c", "d", "e" ]
1679
- # a[2] + a[0] + a[1] #=> "cab"
1680
- # a[6] #=> nil
1681
- # a[1, 2] #=> [ "b", "c" ]
1682
- # a[1..3] #=> [ "b", "c", "d" ]
1683
- # a[4..7] #=> [ "e" ]
1684
- # a[6..10] #=> nil
1685
- # a[-3, 3] #=> [ "c", "d", "e" ]
1686
- # # special cases
1687
- # a[5] #=> nil
1688
- # a[6, 1] #=> nil
1689
- # a[5, 1] #=> []
1690
- # a[5..10] #=> []
1691
- #
1692
- def slice: (int index) -> Elem?
1693
- | (int start, int length) -> Array[Elem]?
1694
- | (Range[Integer] range) -> Array[Elem]?
1695
-
1696
- # Deletes the element(s) given by an `index` (optionally up to `length`
1697
- # elements) or by a `range`.
1698
- #
1699
- # Returns the deleted object (or objects), or `nil` if the `index` is out of
1700
- # range.
1701
- #
1702
- # a = [ "a", "b", "c" ]
1703
- # a.slice!(1) #=> "b"
1704
- # a #=> ["a", "c"]
1705
- # a.slice!(-1) #=> "c"
1706
- # a #=> ["a"]
1707
- # a.slice!(100) #=> nil
1708
- # a #=> ["a"]
1709
- #
1710
- def slice!: (int index) -> Elem?
1711
- | (int start, int length) -> Array[Elem]?
1712
- | (Range[Integer] range) -> Array[Elem]?
1713
-
1714
- # Returns a new array created by sorting `self`.
1715
- #
1716
- # Comparisons for the sort will be done using the `<=>` operator or using an
1717
- # optional code block.
1718
- #
1719
- # The block must implement a comparison between `a` and `b` and return an
1720
- # integer less than 0 when `b` follows `a`, `0` when `a` and `b` are equivalent,
1721
- # or an integer greater than 0 when `a` follows `b`.
1722
- #
1723
- # The result is not guaranteed to be stable. When the comparison of two
1724
- # elements returns `0`, the order of the elements is unpredictable.
1725
- #
1726
- # ary = [ "d", "a", "e", "c", "b" ]
1727
- # ary.sort #=> ["a", "b", "c", "d", "e"]
1728
- # ary.sort {|a, b| b <=> a} #=> ["e", "d", "c", "b", "a"]
1729
- #
1730
- # To produce the reverse order, the following can also be used (and may be
1731
- # faster):
1732
- #
1733
- # ary.sort.reverse! #=> ["e", "d", "c", "b", "a"]
1734
- #
1735
- # See also Enumerable#sort_by.
1736
- #
1737
- def sort: () -> ::Array[Elem]
1738
- | () { (Elem a, Elem b) -> Integer? } -> Array[Elem]
1739
-
1740
- # Sorts `self` in place.
1741
- #
1742
- # Comparisons for the sort will be done using the `<=>` operator or using an
1743
- # optional code block.
1744
- #
1745
- # The block must implement a comparison between `a` and `b` and return an
1746
- # integer less than 0 when `b` follows `a`, `0` when `a` and `b` are equivalent,
1747
- # or an integer greater than 0 when `a` follows `b`.
1748
- #
1749
- # The result is not guaranteed to be stable. When the comparison of two
1750
- # elements returns `0`, the order of the elements is unpredictable.
1751
- #
1752
- # ary = [ "d", "a", "e", "c", "b" ]
1753
- # ary.sort! #=> ["a", "b", "c", "d", "e"]
1754
- # ary.sort! {|a, b| b <=> a} #=> ["e", "d", "c", "b", "a"]
1755
- #
1756
- # See also Enumerable#sort_by.
1757
- #
1758
- def sort!: () -> Array[Elem]
1759
- | () { (Elem a, Elem b) -> Integer? } -> Array[Elem]
1760
-
1761
- # Sorts `self` in place using a set of keys generated by mapping the values in
1762
- # `self` through the given block.
1763
- #
1764
- # The result is not guaranteed to be stable. When two keys are equal, the order
1765
- # of the corresponding elements is unpredictable.
1766
- #
1767
- # If no block is given, an Enumerator is returned instead.
1768
- #
1769
- # See also Enumerable#sort_by.
1770
- #
1771
- def sort_by!: [U] () { (Elem obj) -> U } -> Array[Elem]
1772
- | () -> Enumerator[Elem, Array[Elem]]
1773
-
1774
- # Returns the sum of elements. For example, [e1, e2, e3].sum returns init + e1 +
1775
- # e2 + e3.
1776
- #
1777
- # If a block is given, the block is applied to each element before addition.
1778
- #
1779
- # If *ary* is empty, it returns *init*.
1780
- #
1781
- # [].sum #=> 0
1782
- # [].sum(0.0) #=> 0.0
1783
- # [1, 2, 3].sum #=> 6
1784
- # [3, 5.5].sum #=> 8.5
1785
- # [2.5, 3.0].sum(0.0) {|e| e * e } #=> 15.25
1786
- # [Object.new].sum #=> TypeError
1787
- #
1788
- # The (arithmetic) mean value of an array can be obtained as follows.
1789
- #
1790
- # mean = ary.sum(0.0) / ary.length
1791
- #
1792
- # This method can be used for non-numeric objects by explicit *init* argument.
1793
- #
1794
- # ["a", "b", "c"].sum("") #=> "abc"
1795
- # [[1], [[2]], [3]].sum([]) #=> [1, [2], 3]
1796
- #
1797
- # However, Array#join and Array#flatten is faster than Array#sum for array of
1798
- # strings and array of arrays.
1799
- #
1800
- # ["a", "b", "c"].join #=> "abc"
1801
- # [[1], [[2]], [3]].flatten(1) #=> [1, [2], 3]
1802
- #
1803
- # Array#sum method may not respect method redefinition of "+" methods such as
1804
- # Integer#+.
1805
- #
1806
- def sum: (?untyped init) -> untyped
1807
- | (?untyped init) { (Elem e) -> untyped } -> untyped
1808
-
1809
- # Returns first `n` elements from the array.
1810
- #
1811
- # If a negative number is given, raises an ArgumentError.
1812
- #
1813
- # See also Array#drop
1814
- #
1815
- # a = [1, 2, 3, 4, 5, 0]
1816
- # a.take(3) #=> [1, 2, 3]
1817
- #
1818
- def take: (int n) -> ::Array[Elem]
1819
-
1820
- # Passes elements to the block until the block returns `nil` or `false`, then
1821
- # stops iterating and returns an array of all prior elements.
1822
- #
1823
- # If no block is given, an Enumerator is returned instead.
1824
- #
1825
- # See also Array#drop_while
1826
- #
1827
- # a = [1, 2, 3, 4, 5, 0]
1828
- # a.take_while {|i| i < 3} #=> [1, 2]
1829
- #
1830
- def take_while: () { (Elem obj) -> bool } -> Array[Elem]
1831
- | () -> Enumerator[Elem, Array[Elem]]
1832
-
1833
- # Returns `self`.
1834
- #
1835
- # If called on a subclass of Array, converts the receiver to an Array object.
1836
- #
1837
- def to_a: () -> Array[Elem]
1838
-
1839
- # Returns `self`.
1840
- #
1841
- def to_ary: () -> self
1842
-
1843
- # Returns the result of interpreting *ary* as an array of `[key, value]` pairs.
1844
- #
1845
- # [[:foo, :bar], [1, 2]].to_h
1846
- # # => {:foo => :bar, 1 => 2}
1847
- #
1848
- # If a block is given, the results of the block on each element of the array
1849
- # will be used as pairs.
1850
- #
1851
- # ["foo", "bar"].to_h {|s| [s.ord, s]}
1852
- # # => {102=>"foo", 98=>"bar"}
1853
- #
1854
- def to_h: () -> Hash[untyped, untyped]
1855
- | [T, S] () { (Elem) -> [T, S] } -> Hash[T, S]
1856
-
1857
- alias to_s inspect
1858
-
1859
- # Assumes that `self` is an array of arrays and transposes the rows and columns.
1860
- #
1861
- # a = [[1,2], [3,4], [5,6]]
1862
- # a.transpose #=> [[1, 3, 5], [2, 4, 6]]
1863
- #
1864
- # If the length of the subarrays don't match, an IndexError is raised.
1865
- #
1866
- def transpose: () -> Array[Array[untyped]]
1867
-
1868
- # Set Union --- Returns a new array by joining `other_ary`s with `self`,
1869
- # excluding any duplicates and preserving the order from the given arrays.
1870
- #
1871
- # It compares elements using their #hash and #eql? methods for efficiency.
1872
- #
1873
- # [ "a", "b", "c" ].union( [ "c", "d", "a" ] ) #=> [ "a", "b", "c", "d" ]
1874
- # [ "a" ].union( ["e", "b"], ["a", "c", "b"] ) #=> [ "a", "e", "b", "c" ]
1875
- # [ "a" ].union #=> [ "a" ]
1876
- #
1877
- # See also Array#|.
1878
- #
1879
- def union: [T] (*Array[T] other_arys) -> Array[T | Elem]
1880
-
1881
- # Returns a new array by removing duplicate values in `self`.
1882
- #
1883
- # If a block is given, it will use the return value of the block for comparison.
1884
- #
1885
- # It compares values using their #hash and #eql? methods for efficiency.
1886
- #
1887
- # `self` is traversed in order, and the first occurrence is kept.
1888
- #
1889
- # a = [ "a", "a", "b", "b", "c" ]
1890
- # a.uniq # => ["a", "b", "c"]
1891
- #
1892
- # b = [["student","sam"], ["student","george"], ["teacher","matz"]]
1893
- # b.uniq {|s| s.first} # => [["student", "sam"], ["teacher", "matz"]]
1894
- #
1895
- def uniq: () -> Array[Elem]
1896
- | () { (Elem item) -> untyped } -> Array[Elem]
1897
-
1898
- # Removes duplicate elements from `self`.
1899
- #
1900
- # If a block is given, it will use the return value of the block for comparison.
1901
- #
1902
- # It compares values using their #hash and #eql? methods for efficiency.
1903
- #
1904
- # `self` is traversed in order, and the first occurrence is kept.
1905
- #
1906
- # Returns `nil` if no changes are made (that is, no duplicates are found).
1907
- #
1908
- # a = [ "a", "a", "b", "b", "c" ]
1909
- # a.uniq! # => ["a", "b", "c"]
1910
- #
1911
- # b = [ "a", "b", "c" ]
1912
- # b.uniq! # => nil
1913
- #
1914
- # c = [["student","sam"], ["student","george"], ["teacher","matz"]]
1915
- # c.uniq! {|s| s.first} # => [["student", "sam"], ["teacher", "matz"]]
1916
- #
1917
- def uniq!: () -> Array[Elem]?
1918
- | () { (Elem) -> untyped } -> Array[Elem]?
1919
-
1920
- # Prepends objects to the front of `self`, moving other elements upwards. See
1921
- # also Array#shift for the opposite effect.
1922
- #
1923
- # a = [ "b", "c", "d" ]
1924
- # a.unshift("a") #=> ["a", "b", "c", "d"]
1925
- # a.unshift(1, 2) #=> [ 1, 2, "a", "b", "c", "d"]
1926
- #
1927
- def unshift: (*Elem obj) -> ::Array[Elem]
1928
-
1929
- # Returns an array containing the elements in `self` corresponding to the given
1930
- # `selector`(s).
1931
- #
1932
- # The selectors may be either integer indices or ranges.
1933
- #
1934
- # See also Array#select.
1935
- #
1936
- # a = %w{ a b c d e f }
1937
- # a.values_at(1, 3, 5) # => ["b", "d", "f"]
1938
- # a.values_at(1, 3, 5, 7) # => ["b", "d", "f", nil]
1939
- # a.values_at(-1, -2, -2, -7) # => ["f", "e", "e", nil]
1940
- # a.values_at(4..6, 3...6) # => ["e", "f", nil, "d", "e", "f"]
1941
- #
1942
- def values_at: (*int | Range[Integer] selector) -> Array[Elem?]
1943
-
1944
- # Converts any arguments to arrays, then merges elements of `self` with
1945
- # corresponding elements from each argument.
1946
- #
1947
- # This generates a sequence of `ary.size` *n*-element arrays, where *n* is one
1948
- # more than the count of arguments.
1949
- #
1950
- # If the size of any argument is less than the size of the initial array, `nil`
1951
- # values are supplied.
1952
- #
1953
- # If a block is given, it is invoked for each output `array`, otherwise an array
1954
- # of arrays is returned.
1955
- #
1956
- # a = [ 4, 5, 6 ]
1957
- # b = [ 7, 8, 9 ]
1958
- # [1, 2, 3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
1959
- # [1, 2].zip(a, b) #=> [[1, 4, 7], [2, 5, 8]]
1960
- # a.zip([1, 2], [8]) #=> [[4, 1, 8], [5, 2, nil], [6, nil, nil]]
1961
- #
1962
- def zip: [U] (Array[U] arg) -> Array[[ Elem, U? ]]
1963
- | (Array[untyped] arg, *Array[untyped] args) -> Array[Array[untyped]]
1964
- | [U] (Array[U] arg) { ([Elem, U?]) -> void } -> void
1965
- | (Array[untyped] arg, *Array[untyped] args) { (Array[untyped]) -> void } -> void
1966
-
1967
- # Set Union --- Returns a new array by joining `ary` with `other_ary`, excluding
1968
- # any duplicates and preserving the order from the given arrays.
1969
- #
1970
- # It compares elements using their #hash and #eql? methods for efficiency.
1971
- #
1972
- # [ "a", "b", "c" ] | [ "c", "d", "a" ] #=> [ "a", "b", "c", "d" ]
1973
- # [ "c", "d", "a" ] | [ "a", "b", "c" ] #=> [ "c", "d", "a", "b" ]
1974
- #
1975
- # See also Array#union.
1976
- #
1977
- def |: [T] (Array[T] other_ary) -> Array[Elem | T]
1978
-
1979
- private
1980
-
1981
- # Replaces the contents of `self` with the contents of `other_ary`, truncating
1982
- # or expanding if necessary.
1983
- #
1984
- # a = [ "a", "b", "c", "d", "e" ]
1985
- # a.replace([ "x", "y", "z" ]) #=> ["x", "y", "z"]
1986
- # a #=> ["x", "y", "z"]
1987
- #
1988
- def initialize_copy: (self other_ary) -> void
1989
- end
1990
-
1991
- interface _ToAry[T]
1992
- def to_ary: () -> Array[T]
1993
- end
1994
-
1995
- interface Array::_Pattern[T]
1996
- def ===: (T) -> bool
1997
- end