manager 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHART.html +1270 -0
- data/MANUAL.html +1252 -0
- data/bin/manager +43 -0
- data/examples/array/CHART.html +1376 -0
- data/examples/array/MANUAL.html +1126 -0
- data/examples/array/spec +3438 -0
- data/lib/manager.rb +528 -0
- data/lib/manager/annotation +96 -0
- data/lib/manager/input +189 -0
- data/lib/manager/js +257 -0
- data/lib/manager/refine_module +142 -0
- data/lib/manager/refine_object_mapping +143 -0
- data/lib/manager/refine_test +97 -0
- data/lib/manager/render +1228 -0
- data/lib/manager/spell_check +49 -0
- data/lib/manager/test +404 -0
- data/lib/manager/test_helper +9 -0
- data/license +9 -0
- data/manager.gemspec +21 -0
- data/spec/alternatives_implemented.png +0 -0
- data/spec/alternatives_unimplemented.png +0 -0
- data/spec/annotations.png +0 -0
- data/spec/benchmark_test.png +0 -0
- data/spec/citation.png +0 -0
- data/spec/code_block.png +0 -0
- data/spec/context_module.png +0 -0
- data/spec/documentation +1289 -0
- data/spec/external_link.png +0 -0
- data/spec/image.png +0 -0
- data/spec/list.png +0 -0
- data/spec/long.png +0 -0
- data/spec/main_and_object.png +0 -0
- data/spec/markup.png +0 -0
- data/spec/module_diagram.png +0 -0
- data/spec/navigation.png +0 -0
- data/spec/nested_section_headers.png +0 -0
- data/spec/ruby.png +0 -0
- data/spec/setup_teardown.png +0 -0
- data/spec/short.png +0 -0
- data/spec/signature.png +0 -0
- data/spec/spec +76 -0
- data/spec/spec_example.png +0 -0
- data/spec/table.png +0 -0
- data/spec/test_header.png +0 -0
- data/spec/test_non_unit_spec +184 -0
- data/spec/test_program +71 -0
- data/spec/test_unit_spec +790 -0
- data/spec/tutorial_1.png +0 -0
- data/spec/tutorial_2.png +0 -0
- data/spec/tutorial_3.png +0 -0
- data/spec/tutorial_4.png +0 -0
- data/spec/tutorial_5.png +0 -0
- data/spec/tutorial_6.png +0 -0
- data/spec/tutorial_7.png +0 -0
- data/spec/tutorial_8.png +0 -0
- data/spec/unambiguous_links.png +0 -0
- data/spec/unit_test_failure.png +0 -0
- data/spec/unit_test_raise.png +0 -0
- data/spec/unit_test_receiver.png +0 -0
- data/spec/unit_test_succeed.png +0 -0
- data/spec/unit_test_success.png +0 -0
- data/spec/unit_test_throw.png +0 -0
- data/spec/valid_heading.png +0 -0
- data/spec/with_expr.png +0 -0
- data/spec/without_expr.png +0 -0
- data/theme/2016a.css +670 -0
- data/theme/coderay_github.css +132 -0
- metadata +140 -11
data/examples/array/spec
ADDED
@@ -0,0 +1,3438 @@
|
|
1
|
+
#!ruby
|
2
|
+
# Copy of http://ruby-doc.org/core-2.1.4/Array.html
|
3
|
+
|
4
|
+
Manager.config title: "Array"
|
5
|
+
manage nil
|
6
|
+
|
7
|
+
spec nil,
|
8
|
+
"Arrays are ordered, integer-indexed collections of any object.",
|
9
|
+
"`Array` indexing starts at `0`, as in C or Java. A negative index is assumed to be relative to the end of the array---that is, an index of `-1` indicates the last element of the array, `-2` is the next to last element in the array, and so on.",
|
10
|
+
coda
|
11
|
+
|
12
|
+
spec "=Creating Arrays",
|
13
|
+
"A new array can be created by using the literal constructor `[]`. Arrays can contain different types of objects. For example, the array below contains an `Integer`, a `String` and a `Float`:",
|
14
|
+
<<~'RUBY'.code,
|
15
|
+
ary = [1, "two", 3.0] #=> [1, "two", 3.0]
|
16
|
+
RUBY
|
17
|
+
"An array can also be created by explicitly calling `::new` with zero, one (the initial size of the `Array`), or two arguments (the initial size and a default object).",
|
18
|
+
<<~'RUBY'.code,
|
19
|
+
ary = Array.new #=> []
|
20
|
+
Array.new(3) #=> [nil, nil, nil]
|
21
|
+
Array.new(3, true) #=> [true, true, true]
|
22
|
+
RUBY
|
23
|
+
"Note that the second argument populates the array with references to the same object. Therefore, it is only recommended in cases when you need to instantiate arrays with natively immutable objects such as symbols, numbers, `true`, or `false`.",
|
24
|
+
"To create an array with separate objects, a block can be passed instead. This method is safe to use with mutable objects such as hashes, strings, or other arrays:",
|
25
|
+
<<~'RUBY'.code,
|
26
|
+
Array.new(4){Hash.new} #=> [{}, {}, {}, {}]
|
27
|
+
RUBY
|
28
|
+
"This is also a quick way to build up multi-dimensional arrays:",
|
29
|
+
<<~'RUBY'.code,
|
30
|
+
empty_table = Array.new(3){Array.new(3)}
|
31
|
+
#=> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
|
32
|
+
RUBY
|
33
|
+
"An array can also be created by using the `Array()` method, provided by `Kernel`, which tries to call `to_ary`, then `to_a` on its argument.",
|
34
|
+
<<~'RUBY'.code,
|
35
|
+
Array({:a => "a", :b => "b"}) #=> [[:a, "a"], [:b, "b"]]
|
36
|
+
RUBY
|
37
|
+
coda
|
38
|
+
|
39
|
+
spec "=Example Usage",
|
40
|
+
"In addition to the methods it mixes in through the `Enumerable` module, the `Array` class has proprietary methods for accessing, searching and otherwise manipulating arrays.",
|
41
|
+
"Some of the more common ones are illustrated below.",
|
42
|
+
coda
|
43
|
+
|
44
|
+
spec "=Accessing Elements",
|
45
|
+
"Elements in an array can be retrieved using the `#[]` method. It can take a single integer argument (a numeric index), a pair of arguments (start and length), or a range. Negative indices start counting from the end, with `-1` being the last element.",
|
46
|
+
<<~'RUBY'.code,
|
47
|
+
arr = [1, 2, 3, 4, 5, 6]
|
48
|
+
arr[2] #=> 3
|
49
|
+
arr[100] #=> nil
|
50
|
+
arr[-3] #=> 4
|
51
|
+
arr[2, 3] #=> [3, 4, 5]
|
52
|
+
arr[1..4] #=> [2, 3, 4, 5]
|
53
|
+
arr[1..-3] #=> [2, 3, 4]
|
54
|
+
RUBY
|
55
|
+
"Another way to access a particular array element is by using the at method",
|
56
|
+
<<~'RUBY'.code,
|
57
|
+
arr.at(0) #=> 1
|
58
|
+
RUBY
|
59
|
+
"The slice method works in an identical manner to `#[]`.",
|
60
|
+
"To raise an error for indices outside of the array bounds or else to provide a default value when that happens, you can use `fetch`.",
|
61
|
+
<<~'RUBY'.code,
|
62
|
+
arr = ["a", "b", "c", "d", "e", "f"]
|
63
|
+
arr.fetch(100) #=> IndexError: index 100 outside of array bounds: -6...6
|
64
|
+
arr.fetch(100, "oops") #=> "oops"
|
65
|
+
RUBY
|
66
|
+
"The special methods `first` and `last` will return the first and last elements of an array, respectively.",
|
67
|
+
<<~'RUBY'.code,
|
68
|
+
arr.first #=> 1
|
69
|
+
arr.last #=> 6
|
70
|
+
RUBY
|
71
|
+
"To return the first n elements of an array, use take",
|
72
|
+
<<~'RUBY'.code,
|
73
|
+
arr.take(3) #=> [1, 2, 3]
|
74
|
+
RUBY
|
75
|
+
"`drop` does the opposite of `take`, by returning the elements after n elements have been dropped:",
|
76
|
+
<<~'RUBY'.code,
|
77
|
+
arr.drop(3) #=> [4, 5, 6]
|
78
|
+
RUBY
|
79
|
+
coda
|
80
|
+
|
81
|
+
spec "=Obtaining Information about an Array",
|
82
|
+
"Arrays keep track of their own length at all times. To query an array about the number of elements it contains, use `length`, `count`, or `size`.",
|
83
|
+
<<~'RUBY'.code,
|
84
|
+
browsers = ["Chrome", "Firefox", "Safari", "Opera", "IE"]
|
85
|
+
browsers.length #=> 5
|
86
|
+
browsers.count #=> 5
|
87
|
+
RUBY
|
88
|
+
"To check whether an array contains any elements at all",
|
89
|
+
<<~'RUBY'.code,
|
90
|
+
browsers.empty? #=> false
|
91
|
+
RUBY
|
92
|
+
"To check whether a particular item is included in the array",
|
93
|
+
<<~'RUBY'.code,
|
94
|
+
browsers.include?("Konqueror") #=> false
|
95
|
+
RUBY
|
96
|
+
coda
|
97
|
+
|
98
|
+
spec "=Adding Items to Arrays",
|
99
|
+
"Items can be added to the end of an array by using either `push` or `<<`",
|
100
|
+
<<~'RUBY'.code,
|
101
|
+
arr = [1, 2, 3, 4]
|
102
|
+
arr.push(5) #=> [1, 2, 3, 4, 5]
|
103
|
+
arr << 6 #=> [1, 2, 3, 4, 5, 6]
|
104
|
+
RUBY
|
105
|
+
"`unshift` will add a new item to the beginning of an array.",
|
106
|
+
<<~'RUBY'.code,
|
107
|
+
arr.unshift(0) #=> [0, 1, 2, 3, 4, 5, 6]
|
108
|
+
RUBY
|
109
|
+
"With `insert`, you can add a new element to an array at any position.",
|
110
|
+
<<~'RUBY'.code,
|
111
|
+
arr.insert(3, "apple") #=> [0, 1, 2, "apple", 3, 4, 5, 6]
|
112
|
+
RUBY
|
113
|
+
"Using the `insert` method, you can also insert multiple values at once:",
|
114
|
+
<<~'RUBY'.code,
|
115
|
+
arr.insert(3, "orange", "pear", "grapefruit")
|
116
|
+
#=> [0, 1, 2, "orange", "pear", "grapefruit", "apple", 3, 4, 5, 6]
|
117
|
+
RUBY
|
118
|
+
coda
|
119
|
+
|
120
|
+
spec "=Removing Items from an Array",
|
121
|
+
"The method `pop` removes the last element in an array and returns it:",
|
122
|
+
<<~'RUBY'.code,
|
123
|
+
arr = [1, 2, 3, 4, 5, 6]
|
124
|
+
arr.pop #=> 6
|
125
|
+
arr #=> [1, 2, 3, 4, 5]
|
126
|
+
RUBY
|
127
|
+
"To retrieve and at the same time remove the first item, use `shift`:",
|
128
|
+
<<~'RUBY'.code,
|
129
|
+
arr.shift #=> 1
|
130
|
+
arr #=> [2, 3, 4, 5]
|
131
|
+
RUBY
|
132
|
+
"To delete an element at a particular index:",
|
133
|
+
<<~'RUBY'.code,
|
134
|
+
arr.delete_at(2) #=> 4
|
135
|
+
arr #=> [2, 3, 5]
|
136
|
+
RUBY
|
137
|
+
"To delete a particular element anywhere in an array, use `delete`:",
|
138
|
+
<<~'RUBY'.code,
|
139
|
+
arr = [1, 2, 2, 3]
|
140
|
+
arr.delete(2) #=> 2
|
141
|
+
arr #=> [1,3]
|
142
|
+
RUBY
|
143
|
+
"A useful method if you need to remove `nil` values from an array is `compact`:",
|
144
|
+
<<~'RUBY'.code,
|
145
|
+
arr = ["foo", 0, nil, "bar", 7, "baz", nil]
|
146
|
+
arr.compact #=> ["foo", 0, "bar", 7, "baz"]
|
147
|
+
arr #=> ["foo", 0, nil, "bar", 7, "baz", nil]
|
148
|
+
arr.compact! #=> ["foo", 0, "bar", 7, "baz"]
|
149
|
+
arr #=> ["foo", 0, "bar", 7, "baz"]
|
150
|
+
RUBY
|
151
|
+
"Another common need is to remove duplicate elements from an array.",
|
152
|
+
"It has the non-destructive `uniq`, and destructive method `uniq!`",
|
153
|
+
<<~'RUBY'.code,
|
154
|
+
arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
|
155
|
+
arr.uniq #=> [2, 5, 6, 556, 8, 9, 0, 123]
|
156
|
+
RUBY
|
157
|
+
coda
|
158
|
+
|
159
|
+
spec "=Iterating over Arrays",
|
160
|
+
"Like all classes that include the `Enumerable` module, `Array` has an each method, which defines what elements should be iterated over and how. In case of `Array`’s `each`, all elements in the `Array` instance are yielded to the supplied block in sequence.",
|
161
|
+
"Note that this operation leaves the array unchanged.",
|
162
|
+
<<~'RUBY'.code,
|
163
|
+
arr = [1, 2, 3, 4, 5]
|
164
|
+
arr.each{|a| print a -= 10, " "}
|
165
|
+
# prints: -9 -8 -7 -6 -5
|
166
|
+
#=> [1, 2, 3, 4, 5]
|
167
|
+
RUBY
|
168
|
+
"Another sometimes useful iterator is `reverse_each` which will iterate over the elements in the array in reverse order.",
|
169
|
+
<<~'RUBY'.code,
|
170
|
+
words = %w[first second third fourth fifth sixth]
|
171
|
+
str = ""
|
172
|
+
words.reverse_each{|word| str += "\#\{word\} "}
|
173
|
+
p str #=> "sixth fifth fourth third second first "
|
174
|
+
RUBY
|
175
|
+
"The `map` method can be used to create a new array based on the original array, but with the values modified by the supplied block:",
|
176
|
+
<<~'RUBY'.code,
|
177
|
+
arr.map{|a| 2*a} #=> [2, 4, 6, 8, 10]
|
178
|
+
arr #=> [1, 2, 3, 4, 5]
|
179
|
+
arr.map!{|a| a**2} #=> [1, 4, 9, 16, 25]
|
180
|
+
arr #=> [1, 4, 9, 16, 25]
|
181
|
+
RUBY
|
182
|
+
coda
|
183
|
+
|
184
|
+
spec "=Selecting Items from an Array",
|
185
|
+
"Elements can be selected from an array according to criteria defined in a block. The selection can happen in a destructive or a non-destructive manner. While the destructive operations will modify the array they were called on, the non-destructive methods usually return a new array with the selected elements, but leave the original array unchanged.",
|
186
|
+
coda
|
187
|
+
|
188
|
+
spec "==Non-destructive Selection",
|
189
|
+
<<~'RUBY'.code,
|
190
|
+
arr = [1, 2, 3, 4, 5, 6]
|
191
|
+
arr.select{|a| a > 3} #=> [4, 5, 6]
|
192
|
+
arr.reject{|a| a < 3} #=> [3, 4, 5, 6]
|
193
|
+
arr.drop_while{|a| a < 4} #=> [4, 5, 6]
|
194
|
+
arr #=> [1, 2, 3, 4, 5, 6]
|
195
|
+
RUBY
|
196
|
+
coda
|
197
|
+
|
198
|
+
spec "==Destructive Selection",
|
199
|
+
"`select!` and `reject!` are the corresponding destructive methods to `select` and `reject`",
|
200
|
+
"Similar to `select` vs. `reject`, `delete_if` and `keep_if` have the exact opposite result when supplied with the same block:",
|
201
|
+
<<~'RUBY'.code,
|
202
|
+
arr.delete_if{|a| a < 4} #=> [4, 5, 6]
|
203
|
+
arr #=> [4, 5, 6]
|
204
|
+
arr = [1, 2, 3, 4, 5, 6]
|
205
|
+
arr.keep_if{|a| a < 4} #=> [1, 2, 3]
|
206
|
+
arr #=> [1, 2, 3]
|
207
|
+
RUBY
|
208
|
+
coda
|
209
|
+
|
210
|
+
class Array
|
211
|
+
spec ".[]",
|
212
|
+
{"(*args)" => Array},
|
213
|
+
"Returns a new array populated with the given objects.",
|
214
|
+
<<~'RUBY'.code,
|
215
|
+
Array.[](1, "a", /^A/) # => [1, "a", /^A/]
|
216
|
+
Array[1, "a", /^A/] # => [1, "a", /^A/]
|
217
|
+
[1, "a", /^A/] # => [1, "a", /^A/]
|
218
|
+
RUBY
|
219
|
+
"? ruby/test/ruby/test_array.rb:test_01_square_brackets",
|
220
|
+
Array.UT(5, 4, 3, 2, 1).instance_of?(Array),
|
221
|
+
RETURN.length == 5,
|
222
|
+
*(0..4).map{|i| RETURN[i] == 5 - i},
|
223
|
+
RETURN[6].nil?,
|
224
|
+
coda
|
225
|
+
spec ".new",
|
226
|
+
"Returns a new array.",
|
227
|
+
{"(size = 0, obj = nil)" => Array},
|
228
|
+
"If no arguments are sent, the new array will be empty. When a `size` and an optional `obj` are sent, an array is created with `size` copies of `obj`. Take notice that all elements will reference the same object `obj`.",
|
229
|
+
{"(array)" => Array},
|
230
|
+
"Creates a copy of the array passed as a parameter (the array is generated by calling `#to_ary` on the parameter).",
|
231
|
+
<<~'RUBY'.code,
|
232
|
+
first_array = ["Matz", "Guido"]
|
233
|
+
second_array = Array.new(first_array) #=> ["Matz", "Guido"]
|
234
|
+
first_array.equal? second_array #=> false
|
235
|
+
RUBY
|
236
|
+
{"(size){|index| block}" => Array},
|
237
|
+
"An array of the given `size` is created. Each element in this array is created by passing the element’s index to the given block and storing the return value.",
|
238
|
+
<<~'RUBY'.code,
|
239
|
+
Array.new(3){|index| index ** 2}
|
240
|
+
# => [0, 1, 4]
|
241
|
+
RUBY
|
242
|
+
"? ruby/test/ruby/test_array.rb:test_00_new",
|
243
|
+
Array.UT.instance_of?(Array),
|
244
|
+
RETURN.empty?,
|
245
|
+
RETURN[0].nil?,
|
246
|
+
"Common gotchas",
|
247
|
+
"When sending the second parameter, the same object will be used as the value for all the array elements:",
|
248
|
+
<<~'RUBY'.code,
|
249
|
+
a = Array.new(2, Hash.new)
|
250
|
+
# => [{}, {}]
|
251
|
+
a[0]["cat"] = "feline"
|
252
|
+
a # => [{"cat"=>"feline"}, {"cat"=>"feline"}]
|
253
|
+
a[1]["cat"] = "Felix"
|
254
|
+
a # => [{"cat"=>"Felix"}, {"cat"=>"Felix"}]
|
255
|
+
RUBY
|
256
|
+
"Since all the `Array` elements store the same hash, changes to one of them will affect them all.",
|
257
|
+
"If multiple copies are what you want, you should use the block version which uses the result of that block each time an element of the array needs to be initialized:",
|
258
|
+
<<~'RUBY'.code,
|
259
|
+
a = Array.new(2){Hash.new}
|
260
|
+
a[0]["cat"] = "feline"
|
261
|
+
a # => [{"cat"=>"feline"}, {}]
|
262
|
+
RUBY
|
263
|
+
coda
|
264
|
+
spec ".try_convert",
|
265
|
+
{"(obj)" => Array|value(nil)},
|
266
|
+
"Tries to convert `obj` into an array, using `to_ary` method. Returns the converted array or `nil` if obj cannot be converted for any reason. This method can be used to check if an argument is an array.",
|
267
|
+
<<~'RUBY'.code,
|
268
|
+
Array.try_convert([1]) #=> [1]
|
269
|
+
Array.try_convert("1") #=> nil
|
270
|
+
if tmp = Array.try_convert(arg)
|
271
|
+
# the argument is an array
|
272
|
+
elsif tmp = String.try_convert(arg)
|
273
|
+
# the argument is a string
|
274
|
+
end
|
275
|
+
RUBY
|
276
|
+
"? ruby/test/ruby/test_array.rb:test_try_convert",
|
277
|
+
Array.UT([1]) == [1],
|
278
|
+
Array.UT("1").nil?,
|
279
|
+
coda
|
280
|
+
spec "#&",
|
281
|
+
"See also `[#uniq]`.",
|
282
|
+
{"(other_ary)" => Array},
|
283
|
+
"Set Intersection — Returns a new array containing elements common to the two arrays, excluding any duplicates. The order is preserved from the original array.",
|
284
|
+
"It compares elements using their `hash` and `eql?` methods for efficiency.",
|
285
|
+
<<~'RUBY'.code,
|
286
|
+
[1, 1, 3, 5] & [1, 2, 3] #=> [1, 3]
|
287
|
+
["a", "b", "b", "z"] & ["a", "b", "c"] #=> ["a", "b"]
|
288
|
+
RUBY
|
289
|
+
"? ruby/test/ruby/test_array.rb:test_AND # '&'",
|
290
|
+
Array[1, 1, 3, 5].UT(Array[1, 2, 3]) == Array[1, 3],
|
291
|
+
Array[1, 1, 3, 5].UT(Array[]) == Array[],
|
292
|
+
Array[].UT(Array[1, 2, 3]) == Array[],
|
293
|
+
Array[1, 2, 3].UT(Array[4, 5, 6]) == Array[],
|
294
|
+
"? ruby/test/ruby/test_array.rb:ruby/test/ruby/test_array_andor_0:1/2",
|
295
|
+
[1,2,3].UT([2,4,6]) == [2],
|
296
|
+
"? ruby/test/ruby/test_array.rb:test_0_literal:4/8",
|
297
|
+
[1,2,3].UT([2,3,4]) == [2,3],
|
298
|
+
coda
|
299
|
+
spec "#*",
|
300
|
+
"Repetition",
|
301
|
+
{"(str)" => String},
|
302
|
+
"Equivalent to `self.join(str).`",
|
303
|
+
{"(int)" => Array},
|
304
|
+
"Returns a new array built by concatenating the `int` copies of `self`.",
|
305
|
+
<<~'RUBY'.code,
|
306
|
+
[1, 2, 3] * 3 #=> [1, 2, 3, 1, 2, 3, 1, 2, 3]
|
307
|
+
[1, 2, 3] * "," #=> "1,2,3"
|
308
|
+
RUBY
|
309
|
+
"? ruby/test/ruby/test_array.rb:test_MUL # '*'",
|
310
|
+
Array[].UT(3) == Array[],
|
311
|
+
Array[1].UT(3) == Array[1, 1, 1],
|
312
|
+
Array[1, 2].UT(3) == Array[1, 2, 1, 2, 1, 2],
|
313
|
+
Array[1, 2, 3].UT(0) == Array[],
|
314
|
+
Array[1, 2].UT(-3).raise?(ArgumentError),
|
315
|
+
Array[1, 2, 3, 4, 5].UT("-") == "1-2-3-4-5",
|
316
|
+
Array[1, 2, 3, 4, 5].UT("") == "12345",
|
317
|
+
"? ruby/test/ruby/test_array.rb:test_times",
|
318
|
+
<<~'RUBY'.setup,
|
319
|
+
longp = [127, 63, 31, 15, 7].map{|x| 2**x-1}.find do
|
320
|
+
|x|
|
321
|
+
begin
|
322
|
+
[].first(x)
|
323
|
+
rescue ArgumentError
|
324
|
+
true
|
325
|
+
rescue RangeError
|
326
|
+
false
|
327
|
+
end
|
328
|
+
end
|
329
|
+
RUBY
|
330
|
+
[0, 0, 0, 0].UT((expr('longp') + 1) / 4).raise?(ArgumentError),
|
331
|
+
"? ruby/test/ruby/test_array.rb:test_0_literal:2/8",
|
332
|
+
[1, 2].UT(2) == [1, 2, 1, 2],
|
333
|
+
[1, 2].UT(":") == "1:2",
|
334
|
+
coda
|
335
|
+
spec "#+",
|
336
|
+
"See also `#concat`.",
|
337
|
+
{"(other_ary)" => Array},
|
338
|
+
"Concatenation — Returns a new array built by concatenating the two arrays together to produce a third array.",
|
339
|
+
<<~'RUBY'.code,
|
340
|
+
[1, 2, 3] + [4, 5] #=> [1, 2, 3, 4, 5]
|
341
|
+
a = ["a", "b", "c"]
|
342
|
+
c = a + ["d", "e", "f"]
|
343
|
+
c #=> ["a", "b", "c", "d", "e", "f"]
|
344
|
+
a #=> ["a", "b", "c"]
|
345
|
+
RUBY
|
346
|
+
"? ruby/test/ruby/test_array.rb:test_PLUS # '+'",
|
347
|
+
Array[].UT(Array[]) == Array[],
|
348
|
+
Array[1].UT(Array[]) == Array[1],
|
349
|
+
Array[].UT(Array[1]) == Array[1],
|
350
|
+
Array[1].UT(Array[1]) == Array[1, 1],
|
351
|
+
%w(cat dog).UT((1..3).to_a) == Array["cat", "dog", 1, 2, 3],
|
352
|
+
"? ruby/test/ruby/test_array.rb:test_0_literal:1/8",
|
353
|
+
[1, 2].UT([3, 4]) == [1, 2, 3, 4],
|
354
|
+
coda
|
355
|
+
spec "#-",
|
356
|
+
{"(other_ary)" => Array},
|
357
|
+
"Array Difference",
|
358
|
+
"Returns a new array that is a copy of the original array, removing any items that also appear in `other_ary`. The order is preserved from the original array.",
|
359
|
+
"It compares elements using their `hash` and `eql?` methods for efficiency.",
|
360
|
+
<<~'RUBY'.code,
|
361
|
+
[1, 1, 2, 2, 3, 3, 4, 5] - [1, 2, 4] #=> [3, 3, 5]
|
362
|
+
RUBY
|
363
|
+
"If you need set-like behavior, see the library class `Set`.",
|
364
|
+
"? ruby/test/ruby/test_array.rb:test_MINUS # '-'",
|
365
|
+
Array[1].UT(Array[1]) == Array[],
|
366
|
+
Array[1, 2, 3, 4, 5].UT(Array[2, 3, 4, 5]) == Array[1],
|
367
|
+
Array[1, 2, 1, 3, 1, 4, 1, 5].UT(Array[2, 3, 4, 5]) == Array[1, 1, 1, 1],
|
368
|
+
expr('1000.times.with_object(Array[]){|_, a| a << 1}')
|
369
|
+
.UT(Array[2]) == expr('Array[1] * 1000'),
|
370
|
+
Array[1, 2, 1].UT(Array[2]) == Array[1, 1],
|
371
|
+
Array[1, 2, 3].UT(Array[4, 5, 6]) == Array[1, 2, 3],
|
372
|
+
"? ruby/test/ruby/test_array.rb:test_0_literal:6/8",
|
373
|
+
[1,2,3].UT([2,3]) == [1],
|
374
|
+
coda
|
375
|
+
spec "#<<",
|
376
|
+
{"(obj)" => Array},
|
377
|
+
"Append—Pushes the given object on to the end of this array. This expression returns the array itself, so several appends may be chained together.",
|
378
|
+
<<~'RUBY'.code,
|
379
|
+
[1, 2] << "c" << "d" << [3, 4]
|
380
|
+
#=> [1, 2, "c", "d", [3, 4]]
|
381
|
+
RUBY
|
382
|
+
"? ruby/test/ruby/test_array.rb:test_LSHIFT # '<<'", #'"
|
383
|
+
'a = Array[]'.setup,
|
384
|
+
expr('a').UT(1).succeed?,
|
385
|
+
RECEIVER == Array[1],
|
386
|
+
expr('a').<<(1).<<(2).UT(3).succeed?,
|
387
|
+
RECEIVER == Array[1, 2, 3],
|
388
|
+
expr('a').<<(1).<<(2).<<(3).<<(nil).UT("cat").succeed?,
|
389
|
+
RECEIVER == Array[1, 2, 3, nil, "cat"],
|
390
|
+
expr('a').<<(1).<<(2).<<(3).<<(nil).<<("cat").UT(expr('a')).succeed?,
|
391
|
+
RECEIVER == expr('Array[1, 2, 3, nil, "cat", a]'),
|
392
|
+
coda
|
393
|
+
spec "#<=>",
|
394
|
+
{"(other_ary)" => value(-1)|value(0)|value(+1)|value(nil)},
|
395
|
+
"Comparison — Returns an integer (`-1`, `0`, or `+1`) if this array is less than, equal to, or greater than `other_ary`.",
|
396
|
+
"`nil` is returned if the two values are incomparable.",
|
397
|
+
"Each object in each array is compared (using the `<=>` operator).",
|
398
|
+
"Arrays are compared in an element-wise manner; the first two elements that are not equal will determine the return value for the whole comparison.",
|
399
|
+
"If all the values are equal, then the return is based on a comparison of the array lengths. Thus, two arrays are equal according to `Array#<=>` if, and only if, they have the same length and the value of each element is equal to the value of the corresponding element in the other array.",
|
400
|
+
<<~'RUBY'.code,
|
401
|
+
["a", "a", "c"] <=> ["a", "b", "c"] #=> -1
|
402
|
+
[1, 2, 3, 4, 5, 6] <=> [1, 2] #=> +1
|
403
|
+
RUBY
|
404
|
+
"? ruby/test/ruby/test_array.rb:test_CMP # '<=>'",
|
405
|
+
Array[].UT(Array[]).zero?,
|
406
|
+
Array[1] .UT(Array[1]).zero?,
|
407
|
+
Array[1, 2, 3, "cat"] .UT(Array[1, 2, 3, "cat"]).zero?,
|
408
|
+
Array[] .UT(Array[1]) == -1,
|
409
|
+
Array[1] .UT(Array[]) == 1,
|
410
|
+
Array[1, 2, 3] .UT(Array[1, 2, 3, "cat"]) == -1,
|
411
|
+
Array[1, 2, 3, "cat"] .UT(Array[1, 2, 3]) == 1,
|
412
|
+
Array[1, 2, 3, "cat"] .UT(Array[1, 2, 3, "dog"]) == -1,
|
413
|
+
Array[1, 2, 3, "dog"] .UT(Array[1, 2, 3, "cat"]) == 1,
|
414
|
+
coda
|
415
|
+
spec "#==",
|
416
|
+
{"(other_ary)" =>value(true)|value(false)},
|
417
|
+
"Equality — Two arrays are equal if they contain the same number of elements and if each element is equal to (according to `Object#==`) the corresponding element in `other_ary`.",
|
418
|
+
<<~'RUBY'.code,
|
419
|
+
["a", "c"] == ["a", "c", 7] #=> false
|
420
|
+
["a", "c", 7] == ["a", "c", 7] #=> true
|
421
|
+
["a", "c", 7] == ["a", "d", "f"] #=> false
|
422
|
+
RUBY
|
423
|
+
"? ruby/test/ruby/test_array.rb:test_EQUAL # '=='",
|
424
|
+
Array[].UT(Array[]) == true,
|
425
|
+
Array[1].UT(Array[1]) == true,
|
426
|
+
Array[1, 1, 2, 2].UT(Array[1, 1, 2, 2]) == true,
|
427
|
+
Array[1, 1, 2, 2].UT(Array[1.0, 1.0, 2.0, 2.0]) == true,
|
428
|
+
coda
|
429
|
+
spec "#===",
|
430
|
+
"? ruby/test/ruby/test_array.rb:test_VERY_EQUAL # '==='",
|
431
|
+
Array[].UT(Array[]) == true,
|
432
|
+
Array[1].UT(Array[1]) == true,
|
433
|
+
Array[1, 1, 2, 2].UT(Array[1, 1, 2, 2]) == true,
|
434
|
+
Array[1, 1, 2, 2].UT(Array[1.0, 1.0, 2.0, 2.0]) == true,
|
435
|
+
coda
|
436
|
+
spec "#[]",
|
437
|
+
{"(index)" => Object|value(nil)},
|
438
|
+
{"(start, length)" => Array|value(nil)},
|
439
|
+
{"(range)" => Array|value(nil)},
|
440
|
+
"Element Reference — Returns the element at `index`, or returns a subarray starting at the `start` index and continuing for `length` elements, or returns a subarray specified by `range` of indices.",
|
441
|
+
"Negative indices count backward from the end of the array (`-1` is the last element). For `start` and `range` cases, the starting index is just before an element. Additionally, an empty array is returned when the starting index for an element range is at the end of the array.",
|
442
|
+
"Returns `nil` if the index (or starting index) are out of range.",
|
443
|
+
<<~'RUBY'.code,
|
444
|
+
a = ["a", "b", "c", "d", "e"]
|
445
|
+
a[2] + a[0] + a[1] #=> "cab"
|
446
|
+
a[6] #=> nil
|
447
|
+
a[1, 2] #=> ["b", "c"]
|
448
|
+
a[1..3] #=> ["b", "c", "d"]
|
449
|
+
a[4..7] #=> ["e"]
|
450
|
+
a[6..10] #=> nil
|
451
|
+
a[-3, 3] #=> ["c", "d", "e"]
|
452
|
+
# special cases
|
453
|
+
a[5] #=> nil
|
454
|
+
a[6, 1] #=> nil
|
455
|
+
a[5, 1] #=> []
|
456
|
+
a[5..10] #=> []
|
457
|
+
RUBY
|
458
|
+
"? ruby/test/ruby/test_array.rb:test_AREF # '[]'",
|
459
|
+
'a = Array[*(1..100).to_a]'.setup,
|
460
|
+
expr('a').UT(0) == 1,
|
461
|
+
expr('a').UT(99) == 100,
|
462
|
+
expr('a').UT(100).nil?,
|
463
|
+
expr('a').UT(-1) == 100,
|
464
|
+
expr('a').UT(-2) == 99,
|
465
|
+
expr('a').UT(-100) == 1,
|
466
|
+
expr('a').UT(-101).nil?,
|
467
|
+
expr('a').UT(-101,0).nil?,
|
468
|
+
expr('a').UT(-101,1).nil?,
|
469
|
+
expr('a').UT(-101,-1).nil?,
|
470
|
+
expr('a').UT(10,-1).nil?,
|
471
|
+
expr('a').UT(0,1) == Array[1],
|
472
|
+
expr('a').UT(99,1) == Array[100],
|
473
|
+
expr('a').UT(100,1) == Array[],
|
474
|
+
expr('a').UT(99,100) == Array[100],
|
475
|
+
expr('a').UT(-1,1) == Array[100],
|
476
|
+
expr('a').UT(-2,1) == Array[99],
|
477
|
+
expr('a').UT(-100,0) == Array[],
|
478
|
+
expr('a').UT(-100,1) == Array[1],
|
479
|
+
expr('a').UT(9, 3) == Array[10, 11, 12],
|
480
|
+
expr('a').UT(-91, 3) == Array[10, 11, 12],
|
481
|
+
expr('a').UT(0..0) == Array[1],
|
482
|
+
expr('a').UT(99..99) == Array[100],
|
483
|
+
expr('a').UT(100..100) == Array[],
|
484
|
+
expr('a').UT(99..200) == Array[100],
|
485
|
+
expr('a').UT(-1..-1) == Array[100],
|
486
|
+
expr('a').UT(-2..-2) == Array[99],
|
487
|
+
expr('a').UT(9..11) == Array[10, 11, 12],
|
488
|
+
expr('a').UT(-91..-89) == Array[10, 11, 12],
|
489
|
+
expr('a').UT(10, -3).nil?,
|
490
|
+
expr('a').UT(10..7) == [],
|
491
|
+
expr('a').UT("cat").raise?(TypeError),
|
492
|
+
"? ruby/test/ruby/test_array.rb:test_aref",
|
493
|
+
[].UT(0, 0, 0).raise?(ArgumentError),
|
494
|
+
"? ruby/test/ruby/test_array.rb:test_0_literal:7/8",
|
495
|
+
'x = [0, 1, 2, 3, 4, 5]'.setup,
|
496
|
+
expr('x').UT(2) == 2,
|
497
|
+
expr('x').UT(1..3) == [1, 2, 3],
|
498
|
+
expr('x').UT(1,3) == [1, 2, 3],
|
499
|
+
"? ruby/test/ruby/test_array.rb:test_slice_frozen_array",
|
500
|
+
'a = [1,2,3,4,5]'.setup,
|
501
|
+
expr('a').UT(0,4) == [1,2,3,4],
|
502
|
+
expr('a').UT(1,4) == [2,3,4,5],
|
503
|
+
coda
|
504
|
+
spec "#[]=",
|
505
|
+
"See also `#push`, and `#unshift`.",
|
506
|
+
{"(index, obj)" => Object},
|
507
|
+
{"((start, length), obj)" => Object},
|
508
|
+
{"(range, obj)" => Object},
|
509
|
+
"Element Assignment — Sets the element at `index`, or replaces a subarray from the `start` index for `length` elements, or replaces a subarray specified by the `range` of indices.",
|
510
|
+
"If indices are greater than the current capacity of the array, the array grows automatically. Elements are inserted into the array at `start` if `length` is zero.",
|
511
|
+
"Negative indices will count backward from the end of the array. For `start` and `range` cases, the starting index is just before an element.",
|
512
|
+
"An `IndexError` is raised if a negative index points past the beginning of the array.",
|
513
|
+
<<~'RUBY'.code,
|
514
|
+
a = Array.new
|
515
|
+
a[4] = "4"; #=> [nil, nil, nil, nil, "4"]
|
516
|
+
a[0, 3] = ["a", "b", "c"] #=> ["a", "b", "c", nil, "4"]
|
517
|
+
a[1..2] = [1, 2] #=> ["a", 1, 2, nil, "4"]
|
518
|
+
a[0, 2] = "?" #=> ["?", 2, nil, "4"]
|
519
|
+
a[0..2] = "A" #=> ["A", "4"]
|
520
|
+
a[-1] = "Z" #=> ["A", "Z"]
|
521
|
+
a[1..-1] = nil #=> ["A", nil]
|
522
|
+
a[1..-1] = [] #=> ["A"]
|
523
|
+
a[0, 0] = [1, 2] #=> [1, 2, "A"]
|
524
|
+
a[3, 0] = "B" #=> [1, 2, "A", "B"]
|
525
|
+
RUBY
|
526
|
+
"? ruby/test/ruby/test_array.rb:test_ASET # '[]='",
|
527
|
+
expr('Array[*(0..99).to_a]').UT(0, 0).zero?,
|
528
|
+
RECEIVER == expr('Array[0] + Array[*(1..99).to_a]'),
|
529
|
+
expr('Array[*(0..99).to_a]').UT(10, 10, 0).zero?,
|
530
|
+
RECEIVER == expr('Array[*(0..9).to_a] + Array[0] + Array[*(20..99).to_a]'),
|
531
|
+
expr('Array[*(0..99).to_a]').UT(-1, 0).zero?,
|
532
|
+
RECEIVER == expr('Array[*(0..98).to_a] + Array[0]'),
|
533
|
+
expr('Array[*(0..99).to_a]').UT(-10, 10, 0).zero?,
|
534
|
+
RECEIVER == expr('Array[*(0..89).to_a] + Array[0]'),
|
535
|
+
expr('Array[*(0..99).to_a]').UT(0, 1000, 0).zero?,
|
536
|
+
RECEIVER == Array[0],
|
537
|
+
expr('Array[*(0..99).to_a]').UT(10..19, 0).zero?,
|
538
|
+
RECEIVER == expr('Array[*(0..9).to_a] + Array[0] + Array[*(20..99).to_a]'),
|
539
|
+
'b = Array[*%w(a b c)]'.setup,
|
540
|
+
expr('Array[*(0..99).to_a]').UT(0, 1, expr('b')) == expr('b'),
|
541
|
+
RECEIVER == expr('b + Array[*(1..99).to_a]'),
|
542
|
+
expr('Array[*(0..99).to_a]').UT(10, 10, expr('b')) == expr('b'),
|
543
|
+
RECEIVER == expr('Array[*(0..9).to_a] + b + Array[*(20..99).to_a]'),
|
544
|
+
expr('Array[*(0..99).to_a]').UT(-1, 1, expr('b')) == expr('b'),
|
545
|
+
RECEIVER == expr('Array[*(0..98).to_a] + b'),
|
546
|
+
expr('Array[*(0..99).to_a]').UT(-10, 10, expr('b')) == expr('b'),
|
547
|
+
RECEIVER == expr('Array[*(0..89).to_a] + b'),
|
548
|
+
expr('Array[*(0..99).to_a]').UT(0, 1000, expr('b')) == expr('b'),
|
549
|
+
RECEIVER == expr('b'),
|
550
|
+
expr('Array[*(0..99).to_a]').UT(10..19, expr('b')) == expr('b'),
|
551
|
+
RECEIVER == expr('Array[*(0..9).to_a] + b + Array[*(20..99).to_a]'),
|
552
|
+
'a = Array[1, 2, 3]'.setup,
|
553
|
+
expr('a').UT(1, 0, expr('a')).succeed?,
|
554
|
+
RECEIVER == [1, 1, 2, 3, 2, 3],
|
555
|
+
'a = Array[1, 2, 3]'.setup,
|
556
|
+
expr('a').UT(-1, 0, expr('a')).succeed?,
|
557
|
+
RECEIVER == [1, 2, 1, 2, 3, 3],
|
558
|
+
Array[].UT(5, 0, [5]).succeed?,
|
559
|
+
RECEIVER == [nil, nil, nil, nil, nil, 5],
|
560
|
+
Array[1].UT(1, 0, [2]).succeed?,
|
561
|
+
RECEIVER == [1, 2],
|
562
|
+
Array[1].UT(1, 1, [2]).succeed?,
|
563
|
+
RECEIVER == [1, 2],
|
564
|
+
"? ruby/test/ruby/test_array.rb:test_splice",
|
565
|
+
[0].UT(-2, 0, nil).raise?(IndexError),
|
566
|
+
"? ruby/test/ruby/test_array.rb:test_0_literal:8/8",
|
567
|
+
'x = [0, 1, 2, 3, 4, 5]'.setup,
|
568
|
+
expr('x').UT(0, 2, 10).succeed?,
|
569
|
+
RECEIVER == [10, 2, 3, 4, 5],
|
570
|
+
expr('x').tap{|a| a.[]=(0, 2, 10)}.UT(0, 0, -1).succeed?,
|
571
|
+
RECEIVER == [-1, 10, 2, 3, 4, 5],
|
572
|
+
expr('x').UT(-1, 1, 20).succeed?,
|
573
|
+
RECEIVER[-1] == 20,
|
574
|
+
RECEIVER.pop == 20,
|
575
|
+
coda
|
576
|
+
spec "#assoc",
|
577
|
+
"See also `#rassoc`.",
|
578
|
+
{"(obj)" => Array|value(nil)},
|
579
|
+
"Searches through an array whose elements are also arrays comparing `obj` with the first element of each contained array using `obj.==`.",
|
580
|
+
"Returns the first contained array that matches (that is, the first associated array), or `nil` if no match is found.",
|
581
|
+
<<~'RUBY'.code,
|
582
|
+
s1 = ["colors", "red", "blue", "green"]
|
583
|
+
s2 = ["letters", "a", "b", "c"]
|
584
|
+
s3 = "foo"
|
585
|
+
a = [s1, s2, s3]
|
586
|
+
a.assoc("letters") #=> ["letters", "a", "b", "c"]
|
587
|
+
a.assoc("foo") #=> nil
|
588
|
+
RUBY
|
589
|
+
"? ruby/test/ruby/test_array.rb:test_assoc",
|
590
|
+
<<~'RUBY'.setup,
|
591
|
+
a1 = Array[*%w(cat feline)]
|
592
|
+
a2 = Array[*%w(dog canine)]
|
593
|
+
a3 = Array[*%w(mule asinine)]
|
594
|
+
a4 = Array[a1, a2, a3]
|
595
|
+
RUBY
|
596
|
+
expr('a4').UT("cat") == expr('a1'),
|
597
|
+
expr('a4').UT("mule") == expr('a3'),
|
598
|
+
expr('a4').UT("asinine").nil?,
|
599
|
+
expr('a4').UT("wombat").nil?,
|
600
|
+
expr('a4').UT(1..2).nil?,
|
601
|
+
coda
|
602
|
+
spec "#at",
|
603
|
+
"See also `#[]`.",
|
604
|
+
{"(index)" => Object|value(nil)},
|
605
|
+
"Returns the element at `index`. A negative index counts from the end of `self`. Returns `nil` if the index is out of range.",
|
606
|
+
<<~'RUBY'.code,
|
607
|
+
a = ["a", "b", "c", "d", "e"]
|
608
|
+
a.at(0) #=> "a"
|
609
|
+
a.at(-1) #=> "e"
|
610
|
+
RUBY
|
611
|
+
"? ruby/test/ruby/test_array.rb:test_at",
|
612
|
+
'a = Array[*(0..99).to_a]'.setup,
|
613
|
+
expr('a').UT(0).zero?,
|
614
|
+
expr('a').UT(10) == 10,
|
615
|
+
expr('a').UT(99) == 99,
|
616
|
+
expr('a').UT(100).nil?,
|
617
|
+
expr('a').UT(-1) == 99,
|
618
|
+
expr('a').UT(-100).zero?,
|
619
|
+
expr('a').UT(-101).nil?,
|
620
|
+
expr('a').UT("cat").raise?(TypeError),
|
621
|
+
coda
|
622
|
+
spec "#bsearch",
|
623
|
+
{"{|x| block}" => Object},
|
624
|
+
"By using binary search, finds a value from this array which meets the given condition in `O(log n)` where `n` is the size of the array.",
|
625
|
+
"You can use this method in two use cases: a find-minimum mode and a find-any mode. In either case, the elements of the array must be monotone (or sorted) with respect to the block.",
|
626
|
+
"In find-minimum mode (this is a good choice for typical use case), the block must return `true` or `false`, and there must be an index `i` (`0 <= i <= ary.size`) so that:",
|
627
|
+
"* the block returns `false` for any element whose index is less than `i`, and",
|
628
|
+
"* the block returns `true` for any element whose index is greater than or equal to i.",
|
629
|
+
"This method returns the `i`-th element. If `i` is equal to `ary.size`, it returns `nil`.",
|
630
|
+
<<~'RUBY'.code,
|
631
|
+
ary = [0, 4, 7, 10, 12]
|
632
|
+
ary.bsearch{|x| x >= 4} #=> 4
|
633
|
+
ary.bsearch{|x| x >= 6} #=> 7
|
634
|
+
ary.bsearch{|x| x >= -1} #=> 0
|
635
|
+
ary.bsearch{|x| x >= 100} #=> nil
|
636
|
+
RUBY
|
637
|
+
"In find-any mode (this behaves like libc’s `bsearch(3)`), the block must return a number, and there must be two indices `i` and `j` (`0` <= `i` <= `j` <= `ary.size`) so that:",
|
638
|
+
"* the block returns a positive number for ary if 0 <= k < i,",
|
639
|
+
"* the block returns zero for ary if `i` <= `k` < `j`, and",
|
640
|
+
"* the block returns a negative number for ary if `j` <= `k` < `ary.size`.",
|
641
|
+
"Under this condition, this method returns any element whose index is within `i…j`. If `i` is equal to `j` (i.e., there is no element that satisfies the block), this method returns `nil`.",
|
642
|
+
<<~'RUBY'.code,
|
643
|
+
ary = [0, 4, 7, 10, 12]
|
644
|
+
# try to find v such that 4 <= v < 8
|
645
|
+
ary.bsearch{|x| 1 - x / 4} #=> 4 or 7
|
646
|
+
# try to find v such that 8 <= v < 10
|
647
|
+
ary.bsearch{|x| 4 - x / 2} #=> nil
|
648
|
+
RUBY
|
649
|
+
"You must not mix the two modes at a time; the block must always return either `true`/`false`, or always return a number. It is undefined which value is actually picked up at each iteration.",
|
650
|
+
"? ruby/test/ruby/test_array.rb:test_bsearch_typechecks_return_values",
|
651
|
+
[1, 2, 42, 100, 666].UT{"not ok"}.raise?(TypeError),
|
652
|
+
[1, 2, 42, 100, 666].UT{false} == [1, 2, 42, 100, 666].bsearch{},
|
653
|
+
"? ruby/test/ruby/test_array.rb:test_bsearch_with_no_block",
|
654
|
+
[1, 2, 42, 100, 666].UT.succeed?,
|
655
|
+
RETURN.size.nil?,
|
656
|
+
RETURN.each{|x| x >= 33} == 42,
|
657
|
+
"? ruby/test/ruby/test_array.rb:test_bsearch_in_find_minimum_mode",
|
658
|
+
'a = [0, 4, 7, 10, 12]'.setup,
|
659
|
+
expr('a').UT{|x| x >= 4} == 4,
|
660
|
+
expr('a').UT{|x| x >= 6} == 7,
|
661
|
+
expr('a').UT{|x| x >= -1}.zero?,
|
662
|
+
expr('a').UT{|x| x >= 100}.nil?,
|
663
|
+
"? ruby/test/ruby/test_array.rb:test_bsearch_in_find_any_mode",
|
664
|
+
'a = [0, 4, 7, 10, 12]'.setup,
|
665
|
+
expr('a').UT{|x| 1 - x / 4}.in?([4, 7]),
|
666
|
+
expr('a').UT{|x| 4 - x / 2}.nil?,
|
667
|
+
expr('a').UT{|x| 1}.nil?,
|
668
|
+
expr('a').UT{|x| -1}.nil?,
|
669
|
+
expr('a').UT{|x| (1 - x / 4) * (2**100)}.in?([4, 7]),
|
670
|
+
expr('a').UT{|x| 1 * (2**100)}.nil?,
|
671
|
+
expr('a').UT{|x| (-1) * (2**100)}.nil?,
|
672
|
+
expr('a').UT{|x| (2**100).coerce((1 - x / 4) * (2**100)).first}.in?([4, 7]),
|
673
|
+
coda
|
674
|
+
spec "#clear",
|
675
|
+
{"" => Array},
|
676
|
+
"Removes all elements from `self`.",
|
677
|
+
<<~'RUBY'.code,
|
678
|
+
a = ["a", "b", "c", "d", "e"]
|
679
|
+
a.clear #=> []
|
680
|
+
RUBY
|
681
|
+
"? ruby/test/ruby/test_array.rb:test_clear2",
|
682
|
+
expr('([0] * 1024)').UT == [],
|
683
|
+
"? ruby/test/ruby/test_array.rb:test_clear",
|
684
|
+
'a = Array[1, 2, 3]'.setup,
|
685
|
+
expr('a').UT == Array[],
|
686
|
+
RECEIVER == Array[],
|
687
|
+
RECEIVER == expr('a'),
|
688
|
+
coda
|
689
|
+
spec "#collect",
|
690
|
+
"See also `Enumerable#collect`.",
|
691
|
+
{"{|item| block}" => Array},
|
692
|
+
"Invokes the given block once for each element of `self`.",
|
693
|
+
"Creates a new array containing the values returned by the block.",
|
694
|
+
{"" => Enumerator},
|
695
|
+
<<~'RUBY'.code,
|
696
|
+
a = ["a", "b", "c", "d"]
|
697
|
+
a.collect{|x| x + "!"} #=> ["a!", "b!", "c!", "d!"]
|
698
|
+
a.map.with_index{|x, i| x * i} #=> ["", "b", "cc", "ddd"]
|
699
|
+
a #=> ["a", "b", "c", "d"]
|
700
|
+
RUBY
|
701
|
+
coda
|
702
|
+
spec "#collect!",
|
703
|
+
"See also `Enumerable#collect`.",
|
704
|
+
{"{|item| block}" => Array},
|
705
|
+
"Invokes the given block once for each element of self, replacing the element with the value returned by the block.",
|
706
|
+
{"" => Enumerator},
|
707
|
+
<<~'RUBY'.code,
|
708
|
+
a = ["a", "b", "c", "d"]
|
709
|
+
a.map!{|x| x + "!"}
|
710
|
+
a #=> ["a!", "b!", "c!", "d!"]
|
711
|
+
a.collect!.with_index{|x, i| x[0...i]}
|
712
|
+
a #=> ["", "b", "c!", "d!"]
|
713
|
+
RUBY
|
714
|
+
coda
|
715
|
+
spec "#combination",
|
716
|
+
{"(n){|c| block}" => Array},
|
717
|
+
"When invoked with a block, yields all combinations of length `n` of elements from the array and then returns the array itself.",
|
718
|
+
"The implementation makes no guarantees about the order in which the combinations are yielded.",
|
719
|
+
{"(n)" => Enumerator},
|
720
|
+
"Examples:",
|
721
|
+
<<~'RUBY'.code,
|
722
|
+
a = [1, 2, 3, 4]
|
723
|
+
a.combination(1).to_a #=> [[1],[2],[3],[4]]
|
724
|
+
a.combination(2).to_a #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
|
725
|
+
a.combination(3).to_a #=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
|
726
|
+
a.combination(4).to_a #=> [[1,2,3,4]]
|
727
|
+
a.combination(0).to_a #=> [[]] # one combination of length 0
|
728
|
+
a.combination(5).to_a #=> [] # no combinations of length 5
|
729
|
+
RUBY
|
730
|
+
"? ruby/test/ruby/test_array.rb:test_combination",
|
731
|
+
Array[1,2,3,4].UT(0).to_a == Array[[]],
|
732
|
+
Array[1,2,3,4].UT(1).to_a == Array[[1],[2],[3],[4]],
|
733
|
+
Array[1,2,3,4].UT(2).to_a == Array[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]],
|
734
|
+
Array[1,2,3,4].UT(3).to_a == Array[[1,2,3],[1,2,4],[1,3,4],[2,3,4]],
|
735
|
+
Array[1,2,3,4].UT(4).to_a == Array[[1,2,3,4]],
|
736
|
+
Array[1,2,3,4].UT(5).to_a == Array[],
|
737
|
+
"? ruby/test/ruby/test_array.rb:test_combination2",
|
738
|
+
"?? ruby-core:29240 Must be yielded even if 100C50 > signed integer",
|
739
|
+
(0..100).to_a.UT(50){break :called} == :called,
|
740
|
+
coda
|
741
|
+
spec "#compact",
|
742
|
+
{"" => Array},
|
743
|
+
"Returns a copy of `self` with all `nil` elements removed.",
|
744
|
+
<<~'RUBY'.code,
|
745
|
+
["a", nil, "b", nil, "c", nil].compact
|
746
|
+
#=> ["a", "b", "c"]
|
747
|
+
RUBY
|
748
|
+
"? ruby/test/ruby/test_array.rb:test_compact_0:1/2",
|
749
|
+
[nil, 1, nil, nil, 5, nil, nil].UT == [1, 5],
|
750
|
+
RECEIVER == [nil, 1, nil, nil, 5, nil, nil],
|
751
|
+
"? ruby/test/ruby/test_array.rb:test_compact",
|
752
|
+
Array[1, nil, nil, 2, 3, nil, 4].UT == Array[1, 2, 3, 4],
|
753
|
+
Array[nil, 1, nil, 2, 3, nil, 4].UT == Array[1, 2, 3, 4],
|
754
|
+
Array[1, nil, nil, 2, 3, nil, 4, nil].UT == Array[1, 2, 3, 4],
|
755
|
+
Array[1, 2, 3, 4].UT == Array[1, 2, 3, 4],
|
756
|
+
coda
|
757
|
+
spec "#compact!",
|
758
|
+
{"" => Array|value(nil)},
|
759
|
+
"Removes `nil` elements from the array.",
|
760
|
+
"Returns `nil` if no changes were made, otherwise returns the array.",
|
761
|
+
<<~'RUBY'.code,
|
762
|
+
["a", nil, "b", nil, "c"].compact! #=> ["a", "b", "c"]
|
763
|
+
["a", "b", "c"].compact! #=> nil
|
764
|
+
RUBY
|
765
|
+
"? ruby/test/ruby/test_array.rb:test_compact!",
|
766
|
+
Array[1, nil, nil, 2, 3, nil, 4].UT == Array[1, 2, 3, 4],
|
767
|
+
RECEIVER == Array[1, 2, 3, 4],
|
768
|
+
Array[nil, 1, nil, 2, 3, nil, 4].UT == Array[1, 2, 3, 4],
|
769
|
+
RECEIVER == Array[1, 2, 3, 4],
|
770
|
+
Array[1, nil, nil, 2, 3, nil, 4, nil].UT == Array[1, 2, 3, 4],
|
771
|
+
RECEIVER == Array[1, 2, 3, 4],
|
772
|
+
Array[1, 2, 3, 4].UT.nil?,
|
773
|
+
RECEIVER == Array[1, 2, 3, 4],
|
774
|
+
"? ruby/test/ruby/test_array.rb:test_compact_0:2/2",
|
775
|
+
[nil, 1, nil, nil, 5, nil, nil].UT.succeed?,
|
776
|
+
RECEIVER == [1, 5],
|
777
|
+
coda
|
778
|
+
spec "#concat",
|
779
|
+
"See also `Array#+`.",
|
780
|
+
{"(other_ary)" => Array},
|
781
|
+
"Appends the elements of `other_ary` to `self`.",
|
782
|
+
<<~'RUBY'.code,
|
783
|
+
["a", "b"].concat(["c", "d"]) #=> ["a", "b", "c", "d"]
|
784
|
+
a = [1, 2, 3]
|
785
|
+
a.concat([4, 5])
|
786
|
+
a #=> [1, 2, 3, 4, 5]
|
787
|
+
RUBY
|
788
|
+
"? ruby/test/ruby/test_array.rb:test_concat",
|
789
|
+
Array[1, 2].UT(Array[3, 4]) == Array[1, 2, 3, 4],
|
790
|
+
Array[].UT(Array[1, 2, 3, 4]) == Array[1, 2, 3, 4],
|
791
|
+
Array[1, 2, 3, 4].UT(Array[]) == Array[1, 2, 3, 4],
|
792
|
+
Array[].UT(Array[]) == Array[],
|
793
|
+
Array[Array[1, 2]].UT(Array[Array[3, 4]]) == Array[Array[1, 2], Array[3, 4]],
|
794
|
+
'a = Array[1, 2, 3]'.setup,
|
795
|
+
expr('a').UT(expr('a')).succeed?,
|
796
|
+
RECEIVER == [1, 2, 3, 1, 2, 3],
|
797
|
+
[0].UT(:foo).raise?(TypeError),
|
798
|
+
[0].freeze.UT(:foo).raise?(RuntimeError),
|
799
|
+
coda
|
800
|
+
spec "#count",
|
801
|
+
{"(count)" => Integer},
|
802
|
+
"Returns the number of elements.",
|
803
|
+
{"(obj)" => Integer},
|
804
|
+
"Counts the number of elements which equal `obj` using `==`.",
|
805
|
+
{"{|item| block}" => Integer},
|
806
|
+
"Counts the number of elements for which the block returns a true value.",
|
807
|
+
<<~'RUBY'.code,
|
808
|
+
ary = [1, 2, 4, 2]
|
809
|
+
ary.count #=> 4
|
810
|
+
ary.count(2) #=> 2
|
811
|
+
ary.count{|x| (x % 2).zero?} #=> 3
|
812
|
+
RUBY
|
813
|
+
coda
|
814
|
+
spec "#cycle",
|
815
|
+
{"(n=nil){|obj| block}" => NilClass},
|
816
|
+
"Calls the given block for each element `n` times or forever if `nil` is given.",
|
817
|
+
"Does nothing if a non-positive number is given or the array is empty.",
|
818
|
+
"Returns `nil` if the loop has finished without getting interrupted.",
|
819
|
+
{"(n=nil)" => Enumerator},
|
820
|
+
<<~'RUBY'.code,
|
821
|
+
a = ["a", "b", "c"]
|
822
|
+
a.cycle{|x| puts x} # print, a, b, c, a, b, c,.. forever.
|
823
|
+
a.cycle(2){|x| puts x} # print, a, b, c, a, b, c.
|
824
|
+
RUBY
|
825
|
+
coda
|
826
|
+
spec "#delete",
|
827
|
+
{"(obj)" => Object|value(nil)},
|
828
|
+
"Deletes all items from `self` that are equal to `obj`.",
|
829
|
+
"Returns the last deleted item, or `nil` if no matching item is found.",
|
830
|
+
{"(obj){block}" => Object},
|
831
|
+
"The result of the block is returned if the item is not found. (To remove `nil` elements and get an informative return value, use `#compact!`)",
|
832
|
+
<<~'RUBY'.code,
|
833
|
+
a = ["a", "b", "b", "b", "c"]
|
834
|
+
a.delete("b") #=> "b"
|
835
|
+
a #=> ["a", "c"]
|
836
|
+
a.delete("z") #=> nil
|
837
|
+
a.delete("z"){"not found"} #=> "not found"
|
838
|
+
RUBY
|
839
|
+
"? ruby/test/ruby/test_array.rb:test_delete",
|
840
|
+
expr('Array[*("cab".."cat").to_a]').UT("cap") == "cap",
|
841
|
+
RECEIVER == expr('Array[*("cab".."cao").to_a] + Array[*("caq".."cat").to_a]'),
|
842
|
+
expr('Array[*("cab".."cat").to_a]').UT("cab") == "cab",
|
843
|
+
RECEIVER == expr('Array[*("cac".."cat").to_a]'),
|
844
|
+
expr('Array[*("cab".."cat").to_a]').UT("cat") == "cat",
|
845
|
+
RECEIVER == expr('Array[*("cab".."cas").to_a]'),
|
846
|
+
expr('Array[*("cab".."cat").to_a]').UT("cup").nil?,
|
847
|
+
RECEIVER == expr('Array[*("cab".."cat").to_a]'),
|
848
|
+
expr('Array[*("cab".."cat").to_a]').UT("cup"){99} == 99,
|
849
|
+
RECEIVER == expr('Array[*("cab".."cat").to_a]'),
|
850
|
+
<<~'RUBY'.setup,
|
851
|
+
o = Object.new
|
852
|
+
def o.==(other); true; end
|
853
|
+
o2 = Object.new
|
854
|
+
def o2.==(other); true; end
|
855
|
+
RUBY
|
856
|
+
expr('Array[1, o, o2, 2]').UT(42) == expr('o2'),
|
857
|
+
RECEIVER == [1, 2],
|
858
|
+
coda
|
859
|
+
spec "#delete_at",
|
860
|
+
"See also `#slice!`",
|
861
|
+
{"(index)" => Object|value(nil)},
|
862
|
+
"Deletes the element at the specified `index`, returning that element, or `nil` if the index is out of range.",
|
863
|
+
<<~'RUBY'.code,
|
864
|
+
a = ["ant", "bat", "cat", "dog"]
|
865
|
+
a.delete_at(2) #=> "cat"
|
866
|
+
a #=> ["ant", "bat", "dog"]
|
867
|
+
a.delete_at(99) #=> nil
|
868
|
+
RUBY
|
869
|
+
"? ruby/test/ruby/test_array.rb:test_delete_at",
|
870
|
+
Array[*(1..5).to_a].UT(2) == 3,
|
871
|
+
RECEIVER == Array[1, 2, 4, 5],
|
872
|
+
Array[*(1..5).to_a].UT(-2) == 4,
|
873
|
+
RECEIVER == Array[1, 2, 3, 5],
|
874
|
+
Array[*(1..5).to_a].UT(5).nil?,
|
875
|
+
RECEIVER == Array[1, 2, 3, 4, 5],
|
876
|
+
Array[*(1..5).to_a].UT(-6).nil?,
|
877
|
+
RECEIVER == Array[1, 2, 3, 4, 5],
|
878
|
+
coda
|
879
|
+
spec "#delete_if",
|
880
|
+
"See also `#reject!`",
|
881
|
+
{"{|item| block}" => Array},
|
882
|
+
"Deletes every element of `self` for which block evaluates to `true`.",
|
883
|
+
"The array is changed instantly every time the block is called, not after the iteration is over.",
|
884
|
+
{"" => Enumerator},
|
885
|
+
<<~'RUBY'.code,
|
886
|
+
scores = [97, 42, 75]
|
887
|
+
scores.delete_if{|score| score < 80} #=> [97]
|
888
|
+
RUBY
|
889
|
+
"? ruby/test/ruby/test_array.rb:test_delete_if",
|
890
|
+
'a = Array[1, 2, 3, 4, 5]'.setup,
|
891
|
+
expr('a').UT{false} == expr('a'),
|
892
|
+
RECEIVER == Array[1, 2, 3, 4, 5],
|
893
|
+
'a = Array[1, 2, 3, 4, 5]'.setup,
|
894
|
+
expr('a').UT{true} == expr('a'),
|
895
|
+
RECEIVER == Array[],
|
896
|
+
'a = Array[1, 2, 3, 4, 5]'.setup,
|
897
|
+
expr('a').UT{|i| i > 3} == expr('a'),
|
898
|
+
RECEIVER == Array[1, 2, 3],
|
899
|
+
"?? bug2545 = ruby-core:27366",
|
900
|
+
'a = Array[5, 6, 7, 8, 9, 10]'.setup,
|
901
|
+
expr('a').UT{|i| break i if i > 8; i < 7} == 9,
|
902
|
+
RECEIVER == Array[7, 8, 9, 10],
|
903
|
+
coda
|
904
|
+
spec "#drop",
|
905
|
+
"See also `#take`",
|
906
|
+
{"(n)" => Array},
|
907
|
+
"Drops first `n` elements from `ary` and returns the rest of the elements in an array.",
|
908
|
+
"If a negative number is given, raises an `ArgumentError`.",
|
909
|
+
<<~'RUBY'.code,
|
910
|
+
a = [1, 2, 3, 4, 5, 0]
|
911
|
+
a.drop(3) #=> [4, 5, 0]
|
912
|
+
RUBY
|
913
|
+
"? ruby/test/ruby/test_array.rb:test_drop",
|
914
|
+
[1,2,3,4,5,0].UT(3) == [4,5,0],
|
915
|
+
"?? ruby-dev:34123",
|
916
|
+
[1,2].UT(-1).raise?(ArgumentError),
|
917
|
+
"?? ruby-dev:34123",
|
918
|
+
[1,2].UT(1000000000) == [],
|
919
|
+
coda
|
920
|
+
spec "#drop_while",
|
921
|
+
"See also `#take_while`",
|
922
|
+
{"{|arr| block}" => Array},
|
923
|
+
"Drops elements up to, but not including, the first element for which the block returns `nil` or `false` and returns an array containing the remaining elements.",
|
924
|
+
{"" => Enumerator},
|
925
|
+
<<~'RUBY'.code,
|
926
|
+
a = [1, 2, 3, 4, 5, 0]
|
927
|
+
a.drop_while{|i| i < 3} #=> [3, 4, 5, 0]
|
928
|
+
RUBY
|
929
|
+
coda
|
930
|
+
spec "#each",
|
931
|
+
{"{|item| block}" => Array},
|
932
|
+
"Calls the given block once for each element in `self`, passing that element as a parameter.",
|
933
|
+
{"" => Enumerator},
|
934
|
+
<<~'RUBY'.code,
|
935
|
+
a = ["a", "b", "c"]
|
936
|
+
a.each{|x| print x, " -- "}
|
937
|
+
RUBY
|
938
|
+
"produces:",
|
939
|
+
<<~'RUBY'.code,
|
940
|
+
a -- b -- c --
|
941
|
+
RUBY
|
942
|
+
coda
|
943
|
+
spec "#each_index",
|
944
|
+
{"{|index| block}" => Array},
|
945
|
+
"Same as `#each`, but passes the index of the element instead of the element itself.",
|
946
|
+
{"" => Enumerator},
|
947
|
+
<<~'RUBY'.code,
|
948
|
+
a = ["a", "b", "c"]
|
949
|
+
a.each_index{|x| print x, " -- "}
|
950
|
+
RUBY
|
951
|
+
"produces:",
|
952
|
+
<<~'RUBY'.code,
|
953
|
+
0 -- 1 -- 2 --
|
954
|
+
RUBY
|
955
|
+
coda
|
956
|
+
spec "#empty?",
|
957
|
+
{"" => value(true)|value(false)},
|
958
|
+
"Returns `true` if `self` contains no elements.",
|
959
|
+
<<~'RUBY'.code,
|
960
|
+
[].empty? #=> true
|
961
|
+
RUBY
|
962
|
+
"? ruby/test/ruby/test_array.rb:test_empty_0",
|
963
|
+
[].UT == true,
|
964
|
+
[1].UT == false,
|
965
|
+
[1, 1, 4, 2, 5, 4, 5, 1, 2].UT == false,
|
966
|
+
"? ruby/test/ruby/test_array.rb:test_empty?",
|
967
|
+
Array[].UT == true,
|
968
|
+
Array[1].UT == false,
|
969
|
+
coda
|
970
|
+
spec "#eql?",
|
971
|
+
{"(other)" => value(true)|value(false)},
|
972
|
+
"Returns `true` if `self` and other are the same object, or are both arrays with the same content (according to `Object#eql?`).",
|
973
|
+
"? ruby/test/ruby/test_array.rb:test_eql?",
|
974
|
+
Array[].UT(Array[]) == true,
|
975
|
+
Array[1].UT(Array[1]) == true,
|
976
|
+
Array[1, 1, 2, 2].UT(Array[1, 1, 2, 2]) == true,
|
977
|
+
Array[1, 1, 2, 2].UT(Array[1.0, 1.0, 2.0, 2.0]) == false,
|
978
|
+
coda
|
979
|
+
spec "#fetch",
|
980
|
+
{"(index)" => Object},
|
981
|
+
"Tries to return the element at position `index`, but throws an `IndexError` exception if the referenced `index` lies outside of the array bounds.",
|
982
|
+
{"(index, default)" => Object},
|
983
|
+
"This error is prevented by the `default`, which will act as a default value.",
|
984
|
+
{"(index){|index| block}" => Object},
|
985
|
+
"It will only be executed when an invalid index is referenced. Negative values of index count from the end of the array.",
|
986
|
+
<<~'RUBY'.code,
|
987
|
+
a = [11, 22, 33, 44]
|
988
|
+
a.fetch(1) #=> 22
|
989
|
+
a.fetch(-1) #=> 44
|
990
|
+
a.fetch(4, "cat") #=> "cat"
|
991
|
+
a.fetch(100){|i| puts "\#\{i\} is out of bounds"}
|
992
|
+
#=> "100 is out of bounds"
|
993
|
+
RUBY
|
994
|
+
"? ruby/test/ruby/test_array.rb:test_fetch",
|
995
|
+
[].UT(0, 0){1} == 1,
|
996
|
+
[0, 1].UT(-1) == 1,
|
997
|
+
[0, 1].UT(2).raise?(IndexError),
|
998
|
+
[0, 1].UT(-3).raise?(IndexError),
|
999
|
+
[0, 1].UT(2, 2) == 2,
|
1000
|
+
coda
|
1001
|
+
spec "#fill",
|
1002
|
+
{"(obj)" => Array},
|
1003
|
+
{"(obj, start [, length])" => Array},
|
1004
|
+
{"(obj, range)" => Array},
|
1005
|
+
"Sets the selected elements of `self` (which may be the entire array) to `obj`.",
|
1006
|
+
"A `start` of `nil` is equivalent to zero.",
|
1007
|
+
"A `length` of `nil` is equivalent to the length of the array.",
|
1008
|
+
"Negative values of `start` count from the end of the array, where `-1` is the last element.",
|
1009
|
+
{"{|index| block}" => Array},
|
1010
|
+
{"(start [, length]){|index| block}" => Array},
|
1011
|
+
{"(range){|index| block}" => Array},
|
1012
|
+
"Fills the array with the value of the given block, which is passed the absolute index of each element to be filled.",
|
1013
|
+
<<~'RUBY'.code,
|
1014
|
+
a = ["a", "b", "c", "d"]
|
1015
|
+
a.fill("x") #=> ["x", "x", "x", "x"]
|
1016
|
+
a.fill("z", 2, 2) #=> ["x", "x", "z", "z"]
|
1017
|
+
a.fill("y", 0..1) #=> ["y", "y", "z", "z"]
|
1018
|
+
a.fill{|i| i*i} #=> [0, 1, 4, 9]
|
1019
|
+
a.fill(-2){|i| i*i*i} #=> [0, 1, 8, 27]
|
1020
|
+
RUBY
|
1021
|
+
"? ruby/test/ruby/test_array.rb:test_fill",
|
1022
|
+
Array[].UT(99) == Array[],
|
1023
|
+
Array[].UT(99, 0) == Array[],
|
1024
|
+
Array[].UT(99, 0, 1) == Array[99],
|
1025
|
+
Array[].UT(99, 0..0) == Array[99],
|
1026
|
+
Array[1].UT(99) == Array[99],
|
1027
|
+
Array[1].UT(99, 0) == Array[99],
|
1028
|
+
Array[1].UT(99, 0, 1) == Array[99],
|
1029
|
+
Array[1].UT(99, 0..0) == Array[99],
|
1030
|
+
Array[1, 2].UT(99) == Array[99, 99],
|
1031
|
+
Array[1, 2].UT(99, 0) == Array[99, 99],
|
1032
|
+
Array[1, 2].UT(99, nil) == Array[99, 99],
|
1033
|
+
Array[1, 2].UT(99, 1, nil) == Array[1, 99],
|
1034
|
+
Array[1, 2].UT(99, 0, 1) == Array[99, 2],
|
1035
|
+
Array[1, 2].UT(99, 0..0) == Array[99, 2],
|
1036
|
+
"? ruby/test/ruby/test_array.rb:test_fill_0",
|
1037
|
+
[0, 1, 2, 3, 4, 5].UT(-1) == [-1, -1, -1, -1, -1, -1],
|
1038
|
+
[0, 1, 2, 3, 4, 5].UT(-1, 3) == [0, 1, 2, -1, -1, -1],
|
1039
|
+
[0, 1, 2, 3, 4, 5].UT(-1, 3, 2) == [0, 1, 2, -1, -1, 5],
|
1040
|
+
[0, 1, 2, 3, 4, 5].UT(-1, 3, 5) == [0, 1, 2, -1, -1, -1, -1, -1],
|
1041
|
+
[0, 1, 2, 3, 4, 5].UT(-1, 2, 2) == [0, 1, -1, -1, 4, 5],
|
1042
|
+
[0, 1, 2, 3, 4, 5].UT(-1, 2, 5) == [0, 1, -1, -1, -1, -1, -1],
|
1043
|
+
[0, 1, 2, 3, 4, 5].UT(-1, -2, 1) == [0, 1, 2, 3, -1, 5],
|
1044
|
+
[0, 1, 2, 3, 4, 5].UT(-1, -2, 3) == [0, 1, 2, 3, -1, -1, -1],
|
1045
|
+
[0, 1, 2, 3, 4, 5].UT(-1, 3..4) == [0, 1, 2, -1, -1, 5],
|
1046
|
+
[0, 1, 2, 3, 4, 5].UT(-1, 3...4) == [0, 1, 2, -1, 4, 5],
|
1047
|
+
[0, 1, 2, 3, 4, 5].UT(-1, 2..-2) == [0, 1, -1, -1, -1, 5],
|
1048
|
+
[0, 1, 2, 3, 4, 5].UT(-1, 2...-2) == [0, 1, -1, -1, 4, 5],
|
1049
|
+
[0, 1, 2, 3, 4, 5].UT{|i| i+10} == [10, 11, 12, 13, 14, 15],
|
1050
|
+
[0, 1, 2, 3, 4, 5].UT(3){|i| i+10} == [0, 1, 2, 13, 14, 15],
|
1051
|
+
[0, 1, 2, 3, 4, 5].UT(3, 2){|i| i+10} == [0, 1, 2, 13, 14, 5],
|
1052
|
+
[0, 1, 2, 3, 4, 5].UT(3, 5){|i| i+10} == [0, 1, 2, 13, 14, 15, 16, 17],
|
1053
|
+
[0, 1, 2, 3, 4, 5].UT(3..4){|i| i+10} == [0, 1, 2, 13, 14, 5],
|
1054
|
+
[0, 1, 2, 3, 4, 5].UT(3...4){|i| i+10} == [0, 1, 2, 13, 4, 5],
|
1055
|
+
[0, 1, 2, 3, 4, 5].UT(2..-2){|i| i+10} == [0, 1, 12, 13, 14, 5],
|
1056
|
+
[0, 1, 2, 3, 4, 5].UT(2...-2){|i| i+10} == [0, 1, 12, 13, 4, 5],
|
1057
|
+
"? ruby/test/ruby/test_array.rb:test_fill2",
|
1058
|
+
[].UT(0, 1, expr('longp')).raise?(ArgumentError),
|
1059
|
+
coda
|
1060
|
+
spec "#find_index",
|
1061
|
+
"See also `#rindex`.",
|
1062
|
+
{"(obj)" => Integer|value(nil)},
|
1063
|
+
"Returns the index of the first object in `ary` such that the object is `==` to `obj`. Returns `nil` if no match is found.",
|
1064
|
+
{"{|item| block}" => Integer|value(nil)},
|
1065
|
+
"Returns the index of the first object for which the block returns `true`. Returns `nil` if no match is found.",
|
1066
|
+
{"" => Enumerator},
|
1067
|
+
<<~'RUBY'.code,
|
1068
|
+
a = ["a", "b", "c"]
|
1069
|
+
a.index("b") #=> 1
|
1070
|
+
a.index("z") #=> nil
|
1071
|
+
a.index{|x| x == "b"} #=> 1
|
1072
|
+
RUBY
|
1073
|
+
coda
|
1074
|
+
spec "#first",
|
1075
|
+
"See also `#last` for the opposite effect.",
|
1076
|
+
{"first" => Object|value(nil)},
|
1077
|
+
"Returns the first element of the array. If the array is empty, returns `nil`.",
|
1078
|
+
{"(n)" => Array},
|
1079
|
+
"Returns the first `n` elements of the array. If the array is empty, returns an empty array.",
|
1080
|
+
<<~'RUBY'.code,
|
1081
|
+
a = ["q", "r", "s", "t"]
|
1082
|
+
a.first #=> "q"
|
1083
|
+
a.first(2) #=> ["q", "r"]
|
1084
|
+
RUBY
|
1085
|
+
"? ruby/test/ruby/test_array.rb:test_first",
|
1086
|
+
Array[3, 4, 5].UT == 3,
|
1087
|
+
Array[].UT.nil?,
|
1088
|
+
"? ruby/test/ruby/test_array.rb:test_first2",
|
1089
|
+
[0].UT(2) == [0],
|
1090
|
+
[0].UT(-1).raise?(ArgumentError),
|
1091
|
+
"? ruby/test/ruby/test_array.rb:test_beg_end_0:1/6",
|
1092
|
+
'x = [1, 2, 3, 4, 5]'.setup,
|
1093
|
+
expr('x').UT == 1,
|
1094
|
+
expr('x').UT(1) == [1],
|
1095
|
+
expr('x').UT(3) == [1, 2, 3],
|
1096
|
+
coda
|
1097
|
+
spec "#flatten",
|
1098
|
+
{"" => Array},
|
1099
|
+
"Returns a new array that is a one-dimensional flattening of `self` (recursively). That is, for every element that is an array, extract its elements into the new array.",
|
1100
|
+
{"(level)" => Array},
|
1101
|
+
"The optional `level` argument determines the level of recursion to flatten.",
|
1102
|
+
<<~'RUBY'.code,
|
1103
|
+
s = [1, 2, 3] #=> [1, 2, 3]
|
1104
|
+
t = [4, 5, 6, [7, 8]] #=> [4, 5, 6, [7, 8]]
|
1105
|
+
a = [s, t, 9, 10] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
|
1106
|
+
a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
1107
|
+
a = [1, 2, [3, [4, 5]]]
|
1108
|
+
a.flatten(1) #=> [1, 2, 3, [4, 5]]
|
1109
|
+
RUBY
|
1110
|
+
"? ruby/test/ruby/test_array.rb:test_flatten",
|
1111
|
+
<<~'RUBY'.setup,
|
1112
|
+
a1 = Array[1, 2, 3]
|
1113
|
+
a3 = Array[4, Array[5, 6]]
|
1114
|
+
RUBY
|
1115
|
+
expr('Array[a1, a3]').UT == Array[1, 2, 3, 4, 5, 6],
|
1116
|
+
RECEIVER == expr('Array[a1, a3]'),
|
1117
|
+
expr('Array[a1, Array[], a3]').UT == Array[1, 2, 3, 4, 5, 6],
|
1118
|
+
Array[].UT == Array[],
|
1119
|
+
Array[Array[Array[Array[],Array[]],Array[Array[]],Array[]],Array[Array[Array[]]]]
|
1120
|
+
.UT == Array[],
|
1121
|
+
"?? ruby-dev:31197",
|
1122
|
+
[[]].UT("").raise?(TypeError),
|
1123
|
+
<<~'RUBY'.setup,
|
1124
|
+
a6 = Array[[1, 2], 3]
|
1125
|
+
a6.taint
|
1126
|
+
a8 = Array[[1, 2], 3]
|
1127
|
+
RUBY
|
1128
|
+
expr('a6').UT.tainted?,
|
1129
|
+
expr('a8').UT(0) == expr('a8'),
|
1130
|
+
RETURN.equal?(expr('a8')).!,
|
1131
|
+
coda
|
1132
|
+
spec "#flatten!",
|
1133
|
+
{"" => Array|value(nil)},
|
1134
|
+
"Flattens `self` in place. Returns `nil` if no modifications were made (i.e., the array contains no subarrays.",
|
1135
|
+
{"(level)" => Array|value(nil)},
|
1136
|
+
"The optional `level` argument determines the level of recursion to flatten.",
|
1137
|
+
<<~'RUBY'.code,
|
1138
|
+
a = [1, 2, [3, [4, 5]]]
|
1139
|
+
a.flatten! #=> [1, 2, 3, 4, 5]
|
1140
|
+
a.flatten! #=> nil
|
1141
|
+
a #=> [1, 2, 3, 4, 5]
|
1142
|
+
a = [1, 2, [3, [4, 5]]]
|
1143
|
+
a.flatten!(1) #=> [1, 2, 3, [4, 5]]
|
1144
|
+
RUBY
|
1145
|
+
"? ruby/test/ruby/test_array.rb:test_flatten!",
|
1146
|
+
<<~'RUBY'.setup,
|
1147
|
+
a1 = Array[1, 2, 3]
|
1148
|
+
a3 = Array[4, Array[5, 6]]
|
1149
|
+
a5 = Array[a1, Array[], a3]
|
1150
|
+
RUBY
|
1151
|
+
expr('Array[a1, a3]').UT == Array[1, 2, 3, 4, 5, 6],
|
1152
|
+
RECEIVER == Array[1, 2, 3, 4, 5, 6],
|
1153
|
+
"?? ruby-core:23382",
|
1154
|
+
expr('a5').UT == Array[1, 2, 3, 4, 5, 6],
|
1155
|
+
expr('a5').UT(0).nil?,
|
1156
|
+
RECEIVER == Array[1, 2, 3, 4, 5, 6],
|
1157
|
+
Array[].UT.nil?,
|
1158
|
+
Array[Array[Array[Array[],Array[]],Array[Array[]],Array[]],Array[Array[Array[]]]]
|
1159
|
+
.UT == Array[],
|
1160
|
+
"?? ruby-core:23382",
|
1161
|
+
Array[].UT(0).nil?,
|
1162
|
+
coda
|
1163
|
+
spec "#frozen?",
|
1164
|
+
"See also `Object#frozen?`",
|
1165
|
+
{"" => value(true)|value(false)},
|
1166
|
+
"Returns `true` if this array is frozen (or temporarily frozen while being sorted).",
|
1167
|
+
coda
|
1168
|
+
spec "#hash",
|
1169
|
+
{"" => Fixnum},
|
1170
|
+
"Computes a hash-code for this array. Two arrays with the same content will have the same hash code (and will compare using `eql?`).",
|
1171
|
+
"? ruby/test/ruby/test_array.rb:test_hash",
|
1172
|
+
Array["cat", "dog"].UT == expr("Array[\"cat\", \"dog\"]").hash,
|
1173
|
+
Array["dog", "cat"].UT == expr("Array[\"cat\", \"dog\"]").hash,
|
1174
|
+
"?? bug9231 = ruby-core:58993 Bug #9231",
|
1175
|
+
Array[].UT != expr("false").hash,
|
1176
|
+
"? ruby/test/ruby/test_array.rb:test_0_literal:3/8",
|
1177
|
+
[1, 2].UT == expr("[1, 2]").hash,
|
1178
|
+
coda
|
1179
|
+
spec "#include?",
|
1180
|
+
{"(object)" => value(true)|value(false)},
|
1181
|
+
"Returns `true` if the given object is present in `self` (that is, if any element `==` `object`), otherwise returns `false`.",
|
1182
|
+
<<~'RUBY'.code,
|
1183
|
+
a = ["a", "b", "c"]
|
1184
|
+
a.include?("b") #=> true
|
1185
|
+
a.include?("z") #=> false
|
1186
|
+
RUBY
|
1187
|
+
"? ruby/test/ruby/test_array.rb:test_include?",
|
1188
|
+
'a = Array["cat", 99, /a/, Array[1, 2, 3]]'.setup,
|
1189
|
+
expr('a').UT("cat") == true,
|
1190
|
+
expr('a').UT(99) == true,
|
1191
|
+
expr('a').UT(/a/) == true,
|
1192
|
+
expr('a').UT([1,2,3]) == true,
|
1193
|
+
expr('a').UT("ca") == false,
|
1194
|
+
expr('a').UT([1,2]) == false,
|
1195
|
+
coda
|
1196
|
+
spec "#index",
|
1197
|
+
"See also `#rindex`.",
|
1198
|
+
{"(obj)" => Integer|value(nil)},
|
1199
|
+
"Returns the index of the first object in `self` such that the object is `==` to `obj`.",
|
1200
|
+
{"{|item| block}" => Integer|value(nil)},
|
1201
|
+
"Returns the index of the first object for which the block returns `true`. Returns `nil` if no match is found.",
|
1202
|
+
{"index" => Enumerator},
|
1203
|
+
<<~'RUBY'.code,
|
1204
|
+
a = ["a", "b", "c"]
|
1205
|
+
a.index("b") #=> 1
|
1206
|
+
a.index("z") #=> nil
|
1207
|
+
a.index{|x| x == "b"} #=> 1
|
1208
|
+
RUBY
|
1209
|
+
"? ruby/test/ruby/test_array.rb:test_index",
|
1210
|
+
'a = Array["cat", 99, /a/, 99, Array[1, 2, 3]]'.setup,
|
1211
|
+
expr('a').UT("cat").zero?,
|
1212
|
+
expr('a').UT(99) == 1,
|
1213
|
+
expr('a').UT([1,2,3]) == 4,
|
1214
|
+
expr('a').UT("ca").nil?,
|
1215
|
+
expr('a').UT([1,2]).nil?,
|
1216
|
+
expr('a').UT(99){|x| x == "cat"} == 1,
|
1217
|
+
coda
|
1218
|
+
spec "#initialize_copy",
|
1219
|
+
{"(other_ary)" => Array},
|
1220
|
+
"Replaces the contents of `self` with the contents of `other_ary`, truncating or expanding if necessary.",
|
1221
|
+
<<~'RUBY'.code,
|
1222
|
+
a = ["a", "b", "c", "d", "e"]
|
1223
|
+
a.replace(["x", "y", "z"]) #=> ["x", "y", "z"]
|
1224
|
+
a #=> ["x", "y", "z"]
|
1225
|
+
RUBY
|
1226
|
+
coda
|
1227
|
+
spec "#insert",
|
1228
|
+
{"(index, obj...)" => Array},
|
1229
|
+
"Inserts the given values before the element with the given index.",
|
1230
|
+
"Negative indices count backwards from the end of the array, where `-1` is the last element.",
|
1231
|
+
<<~'RUBY'.code,
|
1232
|
+
a = %w{a b c d}
|
1233
|
+
a.insert(2, 99) #=> ["a", "b", 99, "c", "d"]
|
1234
|
+
a.insert(-2, 1, 2, 3) #=> ["a", "b", 99, "c", 1, 2, 3, "d"]
|
1235
|
+
RUBY
|
1236
|
+
coda
|
1237
|
+
spec "#inspect",
|
1238
|
+
"Also aliased as: `to_s`",
|
1239
|
+
{"" => String},
|
1240
|
+
"Creates a string representation of `self`.",
|
1241
|
+
<<~'RUBY'.code,
|
1242
|
+
["a", "b", "c"].to_s #=> "[\"a\", \"b\", \"c\"]"
|
1243
|
+
RUBY
|
1244
|
+
coda
|
1245
|
+
spec "#join",
|
1246
|
+
{"(separator=$,)" => String},
|
1247
|
+
"Returns a string created by converting each element of the array to a string, separated by the given `separator`. If the `separator` is `nil`, it uses current `$,`. If both the `separator` and `$,` are `nil`, it uses empty string.",
|
1248
|
+
<<~'RUBY'.code,
|
1249
|
+
["a", "b", "c"].join #=> "abc"
|
1250
|
+
["a", "b", "c"].join("-") #=> "a-b-c"
|
1251
|
+
RUBY
|
1252
|
+
"? ruby/test/ruby/test_array.rb:test_join",
|
1253
|
+
<<~'RUBY'.setup,
|
1254
|
+
$, = ""
|
1255
|
+
a = Array[]
|
1256
|
+
RUBY
|
1257
|
+
expr('a').UT == "",
|
1258
|
+
expr('a').UT(",")== "",
|
1259
|
+
expr('a').UT.encoding == Encoding::US_ASCII,
|
1260
|
+
<<~'RUBY'.setup,
|
1261
|
+
$, = ""
|
1262
|
+
a = Array[1, 2]
|
1263
|
+
RUBY
|
1264
|
+
expr('a').UT == "12",
|
1265
|
+
expr('a').UT(nil) == "12",
|
1266
|
+
expr('a').UT(",") == "1,2",
|
1267
|
+
<<~'RUBY'.setup,
|
1268
|
+
$, = ""
|
1269
|
+
a = Array[1, 2, 3]
|
1270
|
+
RUBY
|
1271
|
+
expr('a').UT == "123",
|
1272
|
+
expr('a').UT(nil) == "123",
|
1273
|
+
expr('a').UT(",") == "1,2,3",
|
1274
|
+
<<~'RUBY'.setup,
|
1275
|
+
$, = ":"
|
1276
|
+
a = Array[1, 2, 3]
|
1277
|
+
RUBY
|
1278
|
+
expr('a').UT == "1:2:3",
|
1279
|
+
expr('a').UT(nil) == "1:2:3",
|
1280
|
+
expr('a').UT(",") == "1,2,3",
|
1281
|
+
<<~'RUBY'.setup,
|
1282
|
+
$, = ""
|
1283
|
+
a = Array[1, 2, 3]
|
1284
|
+
a.taint
|
1285
|
+
RUBY
|
1286
|
+
expr('a').UT.tainted?,
|
1287
|
+
"?? bug5902 ruby-core:42161",
|
1288
|
+
'sep = ":".taint'.setup,
|
1289
|
+
Array[].UT(expr('sep')).tainted? == false,
|
1290
|
+
Array[1].UT(expr('sep')).tainted? == false,
|
1291
|
+
Array[1, 2].UT(expr('sep')).tainted?,
|
1292
|
+
<<~'RUBY'.setup,
|
1293
|
+
e = "".force_encoding("EUC-JP")
|
1294
|
+
u = "".force_encoding("UTF-8")
|
1295
|
+
RUBY
|
1296
|
+
[[]].UT.encoding == Encoding::US_ASCII,
|
1297
|
+
expr('[1, [u]]').UT.encoding == Encoding::US_ASCII,
|
1298
|
+
expr('[u, [e]]').UT.encoding == Encoding::UTF_8,
|
1299
|
+
expr('[u, [1]]').UT.encoding == Encoding::UTF_8,
|
1300
|
+
"?? bug5379 ruby-core:39776",
|
1301
|
+
expr('[[], u, nil]').UT.encoding == Encoding::US_ASCII,
|
1302
|
+
[[], "\u3042", nil].UT.encoding == Encoding::UTF_8,
|
1303
|
+
'$, = nil'.setup,
|
1304
|
+
coda
|
1305
|
+
spec "#keep_if",
|
1306
|
+
"See also `#select!`",
|
1307
|
+
{"{|item| block}" => Array},
|
1308
|
+
"Deletes every element of self for which the given block evaluates to `false`.",
|
1309
|
+
{"" => Enumerator},
|
1310
|
+
<<~'RUBY'.code,
|
1311
|
+
a = %w{a b c d e f}
|
1312
|
+
a.keep_if{|v| v =~ /[aeiou]/} #=> ["a", "e"]
|
1313
|
+
RUBY
|
1314
|
+
coda
|
1315
|
+
spec "#last",
|
1316
|
+
"See also `#first` for the opposite effect.",
|
1317
|
+
{"" => Object|value(nil)},
|
1318
|
+
"Returns the last element of `self`. If the array is empty, returns `nil`.",
|
1319
|
+
{"(n)" => Array},
|
1320
|
+
"Returns the last element(s) of `self`.",
|
1321
|
+
<<~'RUBY'.code,
|
1322
|
+
a = ["w", "x", "y", "z"]
|
1323
|
+
a.last #=> "z"
|
1324
|
+
a.last(2) #=> ["y", "z"]
|
1325
|
+
RUBY
|
1326
|
+
"? ruby/test/ruby/test_array.rb:test_last",
|
1327
|
+
Array[].UT.nil?,
|
1328
|
+
Array[1].UT == 1,
|
1329
|
+
'a = Array[*(3..99).to_a]'.setup,
|
1330
|
+
expr('a').UT == 99,
|
1331
|
+
"? ruby/test/ruby/test_array.rb:test_beg_end_0:2/6",
|
1332
|
+
'x = [1, 2, 3, 4, 5]'.setup,
|
1333
|
+
expr('x').UT == 5,
|
1334
|
+
expr('x').UT(1) == [5],
|
1335
|
+
expr('x').UT(3) == [3, 4, 5],
|
1336
|
+
coda
|
1337
|
+
spec "#length",
|
1338
|
+
"Also aliased as: `size`",
|
1339
|
+
{"" => Integer},
|
1340
|
+
"Returns the number of elements in `self`. May be zero.",
|
1341
|
+
<<~'RUBY'.code,
|
1342
|
+
[1, 2, 3, 4, 5].length #=> 5
|
1343
|
+
[].length #=> 0
|
1344
|
+
RUBY
|
1345
|
+
"? ruby/test/ruby/test_array.rb:test_length",
|
1346
|
+
Array[].UT.zero?,
|
1347
|
+
Array[1].UT == 1,
|
1348
|
+
Array[1, nil].UT == 2,
|
1349
|
+
Array[nil, 1].UT == 2,
|
1350
|
+
expr('Array[*(0..233).to_a]').UT == 234,
|
1351
|
+
coda
|
1352
|
+
spec "#map",
|
1353
|
+
"See also `Enumerable#collect`.",
|
1354
|
+
{"{|item| block}" => Array},
|
1355
|
+
"Invokes the given block once for each element of `self`.",
|
1356
|
+
"Creates a new array containing the values returned by the block.",
|
1357
|
+
{"" => Enumerator},
|
1358
|
+
<<~'RUBY'.code,
|
1359
|
+
a = ["a", "b", "c", "d"]
|
1360
|
+
a.collect{|x| x + "!"} #=> ["a!", "b!", "c!", "d!"]
|
1361
|
+
a.map.with_index{|x, i| x * i} #=> ["", "b", "cc", "ddd"]
|
1362
|
+
a #=> ["a", "b", "c", "d"]
|
1363
|
+
RUBY
|
1364
|
+
"? ruby/test/ruby/test_array.rb:test_collect",
|
1365
|
+
Array[1, "cat", 1..1].UT{|e| e.class} == [Fixnum, String, Range],
|
1366
|
+
Array[1, "cat", 1..1].UT{99} == [99, 99, 99],
|
1367
|
+
Array[].UT{99} == [],
|
1368
|
+
Array[1, 2, 3].UT.kind_of?(Enumerator),
|
1369
|
+
coda
|
1370
|
+
spec "#map!",
|
1371
|
+
"See also `Enumerable#collect`.",
|
1372
|
+
{"{|item| block}" => Array},
|
1373
|
+
"Invokes the given block once for each element of `self`, replacing the element with the value returned by the block.",
|
1374
|
+
{"" => Enumerator},
|
1375
|
+
<<~'RUBY'.code,
|
1376
|
+
a = ["a", "b", "c", "d"]
|
1377
|
+
a.map!{|x| x + "!"}
|
1378
|
+
a #=> ["a!", "b!", "c!", "d!"]
|
1379
|
+
a.collect!.with_index{|x, i| x[0...i]}
|
1380
|
+
a #=> ["", "b", "c!", "d!"]
|
1381
|
+
RUBY
|
1382
|
+
"? ruby/test/ruby/test_array.rb:test_collect!",
|
1383
|
+
Array[1, "cat", 1..1].UT{|e| e.class} == [Fixnum, String, Range],
|
1384
|
+
RECEIVER == [Fixnum, String, Range],
|
1385
|
+
Array[1, "cat", 1..1].UT{99} == [99, 99, 99],
|
1386
|
+
RECEIVER == [99, 99, 99],
|
1387
|
+
Array[].UT{99} == [],
|
1388
|
+
RECEIVER == [],
|
1389
|
+
"? ruby/test/ruby/test_array.rb:test_map!",
|
1390
|
+
Array[1, "cat", 1..1].UT{|e| e.class} == Array[Fixnum, String, Range],
|
1391
|
+
RECEIVER == Array[Fixnum, String, Range],
|
1392
|
+
Array[1, "cat", 1..1].UT{99} == Array[99, 99, 99],
|
1393
|
+
RECEIVER == Array[99, 99, 99],
|
1394
|
+
Array[].UT{99} == Array[],
|
1395
|
+
RECEIVER == Array[],
|
1396
|
+
coda
|
1397
|
+
spec "#pack",
|
1398
|
+
"See also `String#unpack`.",
|
1399
|
+
{"(aTemplateString)" => String},
|
1400
|
+
"Packs the contents of `self` into a binary sequence according to the directives in `aTemplateString` (see the table below) Directives `A`, `a`, and `Z` may be followed by a count, which gives the width of the resulting field. The remaining directives also may take a count, indicating the number of array elements to convert. If the count is an asterisk (`*`), all remaining array elements will be converted. Any of the directives `sSiIlL` may be followed by an underscore (`_`) or exclamation mark (`!`) to use the underlying platform’s native size for the specified type; otherwise, they use a platform-independent size. Spaces are ignored in the template string.",
|
1401
|
+
<<~'RUBY'.code,
|
1402
|
+
a = ["a", "b", "c"]
|
1403
|
+
n = [65, 66, 67]
|
1404
|
+
a.pack("A3A3A3") #=> "a b c "
|
1405
|
+
a.pack("a3a3a3") #=> "a\000\000b\000\000c\000\000"
|
1406
|
+
n.pack("ccc") #=> "ABC"
|
1407
|
+
RUBY
|
1408
|
+
"Directives for pack.",
|
1409
|
+
<<~'RUBY'.code,
|
1410
|
+
Integer | Array |
|
1411
|
+
Directive | Element | Meaning
|
1412
|
+
---------------------------------------------------------------------------
|
1413
|
+
C | Integer | 8-bit unsigned (unsigned char)
|
1414
|
+
S | Integer | 16-bit unsigned, native endian (uint16_t)
|
1415
|
+
L | Integer | 32-bit unsigned, native endian (uint32_t)
|
1416
|
+
Q | Integer | 64-bit unsigned, native endian (uint64_t)
|
1417
|
+
| |
|
1418
|
+
c | Integer | 8-bit signed (signed char)
|
1419
|
+
s | Integer | 16-bit signed, native endian (int16_t)
|
1420
|
+
l | Integer | 32-bit signed, native endian (int32_t)
|
1421
|
+
q | Integer | 64-bit signed, native endian (int64_t)
|
1422
|
+
| |
|
1423
|
+
S_, S! | Integer | unsigned short, native endian
|
1424
|
+
I, I_, I! | Integer | unsigned int, native endian
|
1425
|
+
L_, L! | Integer | unsigned long, native endian
|
1426
|
+
Q_, Q! | Integer | unsigned long long, native endian (ArgumentError
|
1427
|
+
| | if the platform has no long long type.)
|
1428
|
+
| | (Q_ and Q! is available since Ruby 2.1.)
|
1429
|
+
| |
|
1430
|
+
s_, s! | Integer | signed short, native endian
|
1431
|
+
i, i_, i! | Integer | signed int, native endian
|
1432
|
+
l_, l! | Integer | signed long, native endian
|
1433
|
+
q_, q! | Integer | signed long long, native endian (ArgumentError
|
1434
|
+
| | if the platform has no long long type.)
|
1435
|
+
| | (q_ and q! is available since Ruby 2.1.)
|
1436
|
+
| |
|
1437
|
+
S> L> Q> | Integer | same as the directives without ">" except
|
1438
|
+
s> l> q> | | big endian
|
1439
|
+
S!> I!> | | (available since Ruby 1.9.3)
|
1440
|
+
L!> Q!> | | "S>" is same as "n"
|
1441
|
+
s!> i!> | | "L>" is same as "N"
|
1442
|
+
l!> q!> | |
|
1443
|
+
| |
|
1444
|
+
S< L< Q< | Integer | same as the directives without "<" except
|
1445
|
+
s< l< q< | | little endian
|
1446
|
+
S!< I!< | | (available since Ruby 1.9.3)
|
1447
|
+
L!< Q!< | | "S<" is same as "v"
|
1448
|
+
s!< i!< | | "L<" is same as "V"
|
1449
|
+
l!< q!< | |
|
1450
|
+
| |
|
1451
|
+
n | Integer | 16-bit unsigned, network (big-endian) byte order
|
1452
|
+
N | Integer | 32-bit unsigned, network (big-endian) byte order
|
1453
|
+
v | Integer | 16-bit unsigned, VAX (little-endian) byte order
|
1454
|
+
V | Integer | 32-bit unsigned, VAX (little-endian) byte order
|
1455
|
+
| |
|
1456
|
+
U | Integer | UTF-8 character
|
1457
|
+
w | Integer | BER-compressed integer
|
1458
|
+
|
1459
|
+
Float | |
|
1460
|
+
Directive | | Meaning
|
1461
|
+
---------------------------------------------------------------------------
|
1462
|
+
D, d | Float | double-precision, native format
|
1463
|
+
F, f | Float | single-precision, native format
|
1464
|
+
E | Float | double-precision, little-endian byte order
|
1465
|
+
e | Float | single-precision, little-endian byte order
|
1466
|
+
G | Float | double-precision, network (big-endian) byte order
|
1467
|
+
g | Float | single-precision, network (big-endian) byte order
|
1468
|
+
|
1469
|
+
String | |
|
1470
|
+
Directive | | Meaning
|
1471
|
+
---------------------------------------------------------------------------
|
1472
|
+
A | String | arbitrary binary string (space padded, count is width)
|
1473
|
+
a | String | arbitrary binary string (null padded, count is width)
|
1474
|
+
Z | String | same as ``a'', except that null is added with *
|
1475
|
+
B | String | bit string (MSB first)
|
1476
|
+
b | String | bit string (LSB first)
|
1477
|
+
H | String | hex string (high nibble first)
|
1478
|
+
h | String | hex string (low nibble first)
|
1479
|
+
u | String | UU-encoded string
|
1480
|
+
M | String | quoted printable, MIME encoding (see RFC2045)
|
1481
|
+
m | String | base64 encoded string (see RFC 2045, count is width)
|
1482
|
+
| | (if count is 0, no line feed are added, see RFC 4648)
|
1483
|
+
P | String | pointer to a structure (fixed-length string)
|
1484
|
+
p | String | pointer to a null-terminated string
|
1485
|
+
|
1486
|
+
Misc. | |
|
1487
|
+
Directive | | Meaning
|
1488
|
+
---------------------------------------------------------------------------
|
1489
|
+
@ | --- | moves to absolute position
|
1490
|
+
X | --- | back up a byte
|
1491
|
+
x | --- | null byte
|
1492
|
+
RUBY
|
1493
|
+
[
|
1494
|
+
[" Integer Directive ", " Array Element ", " Meaning "],
|
1495
|
+
["C ", "Integer ", "8-bit unsigned (unsigned char)"],
|
1496
|
+
["S ", "Integer ", "16-bit unsigned, native endian (uint16_t)"],
|
1497
|
+
["L ", "Integer ", "32-bit unsigned, native endian (uint32_t)"],
|
1498
|
+
["Q ", "Integer ", "64-bit unsigned, native endian (uint64_t)"],
|
1499
|
+
["c ", "Integer ", "8-bit signed (signed char)"],
|
1500
|
+
["s ", "Integer ", "16-bit signed, native endian (int16_t)"],
|
1501
|
+
["l ", "Integer ", "32-bit signed, native endian (int32_t)"],
|
1502
|
+
["q ", "Integer ", "64-bit signed, native endian (int64_t)"],
|
1503
|
+
["S_, S! ", "Integer ", "unsigned short, native endian"],
|
1504
|
+
["I, I_, I! ", "Integer ", "unsigned int, native endian"],
|
1505
|
+
["L_, L! ", "Integer ", "unsigned long, native endian"],
|
1506
|
+
["Q_, Q! ", "Integer ", "unsigned long long, native endian (ArgumentError
|
1507
|
+
if the platform has no long long type. Q_ and Q! are available since Ruby 2.1.)"],
|
1508
|
+
["s_, s! ", "Integer ", "signed short, native endian"],
|
1509
|
+
["i, i_, i! ", "Integer ", "signed int, native endian"],
|
1510
|
+
["l_, l! ", "Integer ", "signed long, native endian"],
|
1511
|
+
["q_, q! ", "Integer ", "signed long long, native endian (ArgumentError
|
1512
|
+
if the platform has no long long type. q_ and q! are available since Ruby 2.1.)"],
|
1513
|
+
["S> L> Q> ", "Integer ", "same as the directives without \">\" except"],
|
1514
|
+
["s> l> q> ", "", "big endian"],
|
1515
|
+
],
|
1516
|
+
=begin
|
1517
|
+
],
|
1518
|
+
S!> I!> | | (available since Ruby 1.9.3)
|
1519
|
+
L!> Q!> | | "S>" is same as "n"
|
1520
|
+
s!> i!> | | "L>" is same as "N"
|
1521
|
+
l!> q!> | |
|
1522
|
+
| |
|
1523
|
+
S< L< Q< | Integer | same as the directives without "<" except
|
1524
|
+
s< l< q< | | little endian
|
1525
|
+
S!< I!< | | (available since Ruby 1.9.3)
|
1526
|
+
L!< Q!< | | "S<" is same as "v"
|
1527
|
+
s!< i!< | | "L<" is same as "V"
|
1528
|
+
l!< q!< | |
|
1529
|
+
| |
|
1530
|
+
n | Integer | 16-bit unsigned, network (big-endian) byte order
|
1531
|
+
N | Integer | 32-bit unsigned, network (big-endian) byte order
|
1532
|
+
v | Integer | 16-bit unsigned, VAX (little-endian) byte order
|
1533
|
+
V | Integer | 32-bit unsigned, VAX (little-endian) byte order
|
1534
|
+
| |
|
1535
|
+
U | Integer | UTF-8 character
|
1536
|
+
w | Integer | BER-compressed integer
|
1537
|
+
|
1538
|
+
Float | |
|
1539
|
+
Directive | | Meaning
|
1540
|
+
---------------------------------------------------------------------------
|
1541
|
+
D, d | Float | double-precision, native format
|
1542
|
+
F, f | Float | single-precision, native format
|
1543
|
+
E | Float | double-precision, little-endian byte order
|
1544
|
+
e | Float | single-precision, little-endian byte order
|
1545
|
+
G | Float | double-precision, network (big-endian) byte order
|
1546
|
+
g | Float | single-precision, network (big-endian) byte order
|
1547
|
+
|
1548
|
+
String | |
|
1549
|
+
Directive | | Meaning
|
1550
|
+
---------------------------------------------------------------------------
|
1551
|
+
A | String | arbitrary binary string (space padded, count is width)
|
1552
|
+
a | String | arbitrary binary string (null padded, count is width)
|
1553
|
+
Z | String | same as ``a'', except that null is added with *
|
1554
|
+
B | String | bit string (MSB first)
|
1555
|
+
b | String | bit string (LSB first)
|
1556
|
+
H | String | hex string (high nibble first)
|
1557
|
+
h | String | hex string (low nibble first)
|
1558
|
+
u | String | UU-encoded string
|
1559
|
+
M | String | quoted printable, MIME encoding (see RFC2045)
|
1560
|
+
m | String | base64 encoded string (see RFC 2045, count is width)
|
1561
|
+
| | (if count is 0, no line feed are added, see RFC 4648)
|
1562
|
+
P | String | pointer to a structure (fixed-length string)
|
1563
|
+
p | String | pointer to a null-terminated string
|
1564
|
+
|
1565
|
+
Misc. | |
|
1566
|
+
Directive | | Meaning
|
1567
|
+
---------------------------------------------------------------------------
|
1568
|
+
@ | --- | moves to absolute position
|
1569
|
+
X | --- | back up a byte
|
1570
|
+
x | --- | null byte
|
1571
|
+
=end
|
1572
|
+
"? ruby/test/ruby/test_array.rb:test_pack",
|
1573
|
+
'a = Array[*%w(cat wombat x yy)]'.setup,
|
1574
|
+
expr('a').UT("A3A3A3A3") == "catwomx yy ",
|
1575
|
+
expr('a').UT("A*") == "cat",
|
1576
|
+
expr('a').UT("A3@1A3@2A3A3") == "cwx yy ",
|
1577
|
+
expr('a').UT("a3a3a3a3") == "catwomx\000\000yy\000",
|
1578
|
+
expr('a').UT("a*") == "cat",
|
1579
|
+
expr('a').UT("a2") == "ca",
|
1580
|
+
expr('a').UT("a5") == "cat\000\000",
|
1581
|
+
Array["01100001"].UT("B8") == "\x61",
|
1582
|
+
Array["01100001"].UT("B*") == "\x61",
|
1583
|
+
Array["0110000100110111"].UT("B8") == "\x61",
|
1584
|
+
Array["0110000100110111"].UT("B16") == "\x61\x37",
|
1585
|
+
Array["01100001", "00110111"].UT("B8B8") == "\x61\x37",
|
1586
|
+
Array["01100001"].UT("B4") == "\x60",
|
1587
|
+
Array["01100001"].UT("B2") == "\x40",
|
1588
|
+
Array["01100001"].UT("b8") == "\x86",
|
1589
|
+
Array["01100001"].UT("b*") == "\x86",
|
1590
|
+
Array["0110000100110111"].UT("b8") == "\x86",
|
1591
|
+
Array["0110000100110111"].UT("b16") == "\x86\xec",
|
1592
|
+
Array["01100001", "00110111"].UT("b8b8") == "\x86\xec",
|
1593
|
+
Array["01100001"].UT("b4") == "\x06",
|
1594
|
+
Array["01100001"].UT("b2") == "\x02",
|
1595
|
+
Array[65, 66, 67].UT("C3") == "ABC",
|
1596
|
+
Array[-1, 66, 67].UT("C*") == "\377BC",
|
1597
|
+
Array[65, 66, 67].UT("c3") == "ABC",
|
1598
|
+
Array[-1, 66, 67].UT("c*") == "\377BC",
|
1599
|
+
Array["4142", "0a", "12"].UT("H4H2H1") == "AB\n\x10",
|
1600
|
+
Array["1424", "a0", "21"].UT("h4h2h1") == "AB\n\x02",
|
1601
|
+
Array["abc\002def", "cat", "\001"].UT("M9M3M4") == "abc=02def=\ncat=\n=01=\n",
|
1602
|
+
Array["hello\n"].UT("m") == "aGVsbG8K\n",
|
1603
|
+
Array["hello\nhello\n"].UT("u") == ",:&5L;&\\*:&5L;&\\*\n",
|
1604
|
+
Array[0xa9, 0x42, 0x2260].UT("U*") == "\u{a9 42 2260}",
|
1605
|
+
"?? Need the expression in here to force `ary[5]` to be numeric. This avoids `test2` failing because `ary2` goes str->numeric->str and `ary` does not.",
|
1606
|
+
<<~'RUBY'.setup,
|
1607
|
+
format = "c2x5CCxsdils_l_a6"
|
1608
|
+
ary = [1, -100, 127, 128, 32767, 987.654321098/100.0, 12345, 123456, -32767, -123456,
|
1609
|
+
"abcdef"]
|
1610
|
+
RUBY
|
1611
|
+
expr('ary').UT(expr('format')) !~ /def/,
|
1612
|
+
RETURN.unpack(expr("format")).length == expr('ary').length,
|
1613
|
+
RETURN.unpack(expr("format")).join(":") == expr('ary').join(':'),
|
1614
|
+
coda
|
1615
|
+
spec "#permutation",
|
1616
|
+
"The implementation makes no guarantees about the order in which the permutations are yielded.",
|
1617
|
+
{"(n){|p| block}" => Array},
|
1618
|
+
"Yields all permutations of length `n` of the elements of the array, then return the array itself.",
|
1619
|
+
{"{|p| block}" => Array},
|
1620
|
+
"Yields all permutations of all elements.",
|
1621
|
+
{"(n)" => Enumerator},
|
1622
|
+
{"" => Enumerator},
|
1623
|
+
"Examples:",
|
1624
|
+
<<~'RUBY'.code,
|
1625
|
+
a = [1, 2, 3]
|
1626
|
+
a.permutation.to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
|
1627
|
+
a.permutation(1).to_a #=> [[1],[2],[3]]
|
1628
|
+
a.permutation(2).to_a #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
|
1629
|
+
a.permutation(3).to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
|
1630
|
+
a.permutation(0).to_a #=> [[]] # one permutation of length 0
|
1631
|
+
a.permutation(4).to_a #=> [] # no permutations of length 4
|
1632
|
+
RUBY
|
1633
|
+
"? ruby/test/ruby/test_array.rb:test_permutation",
|
1634
|
+
'a = Array[1,2,3]'.setup,
|
1635
|
+
expr('a').UT(0).to_a == Array[[]],
|
1636
|
+
expr('a').UT(1).to_a.sort == Array[[1],[2],[3]],
|
1637
|
+
expr('a').UT(2).to_a.sort == Array[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]],
|
1638
|
+
expr('a').UT(3)
|
1639
|
+
.sort.to_a == Array[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]],
|
1640
|
+
expr('a').UT(4).to_a == Array[],
|
1641
|
+
expr('a').UT(-1).to_a == Array[],
|
1642
|
+
'a = "edcba".each_char.to_a'.setup,
|
1643
|
+
expr('a').UT(5).sort == expr("'abcde'").each_char.to_a.permutation(5).sort,
|
1644
|
+
Array[].UT(0).to_a == Array[[]],
|
1645
|
+
<<~'RUBY'.setup,
|
1646
|
+
a = Array[1, 2, 3, 4]
|
1647
|
+
b = Array[]
|
1648
|
+
RUBY
|
1649
|
+
expr('a').UT{|x| expr('b') << x; expr('a').replace(Array[9, 8, 7, 6])} == Array[9, 8, 7, 6],
|
1650
|
+
Array[1, 2, 3, 4].UT.to_a == expr("b"),
|
1651
|
+
"?? bug3708 ruby-dev:42067",
|
1652
|
+
Array[0, 1, 2, 3, 4][1, 4].UT.to_a == expr("b"),
|
1653
|
+
"? bug9932 ruby-core:63103 Bug #9932",
|
1654
|
+
=begin
|
1655
|
+
assert_separately([], <<~"end;") # do
|
1656
|
+
assert_nothing_raised(SystemStackError) do
|
1657
|
+
Array.new(100_000, nil).UT{break :ok} == :ok
|
1658
|
+
end
|
1659
|
+
end
|
1660
|
+
=end
|
1661
|
+
coda
|
1662
|
+
spec "#pop",
|
1663
|
+
"See also `#push` for the opposite effect.",
|
1664
|
+
{"" => Object|value(nil)},
|
1665
|
+
"Removes the last element from `self` and returns it, or `nil` if the array is empty.",
|
1666
|
+
{"(n)" => Array},
|
1667
|
+
"Returns an array of the last `n` elements (or less) just like `array.slice!(-n, n)` does.",
|
1668
|
+
<<~'RUBY'.code,
|
1669
|
+
a = ["a", "b", "c", "d"]
|
1670
|
+
a.pop #=> "d"
|
1671
|
+
a.pop(2) #=> ["b", "c"]
|
1672
|
+
a #=> ["a"]
|
1673
|
+
RUBY
|
1674
|
+
"? ruby/test/ruby/test_array.rb:test_pop",
|
1675
|
+
'a = Array["cat", "dog"]'.setup,
|
1676
|
+
expr('a').UT == "dog",
|
1677
|
+
RECEIVER == Array["cat"],
|
1678
|
+
expr('a').UT == "cat",
|
1679
|
+
RECEIVER == Array[],
|
1680
|
+
expr('a').UT.nil?,
|
1681
|
+
RECEIVER == Array[],
|
1682
|
+
"? ruby/test/ruby/test_array.rb:test_beg_end_0:5/6",
|
1683
|
+
'x = [1, 2, 3, 4, 5]'.setup,
|
1684
|
+
expr('x').UT == 5,
|
1685
|
+
expr('x').UT(2) == [3, 4],
|
1686
|
+
RECEIVER == [1, 2],
|
1687
|
+
coda
|
1688
|
+
spec "#product",
|
1689
|
+
{"(other_ary, ...)" => Array},
|
1690
|
+
"Returns an array of all combinations of elements from all arrays.",
|
1691
|
+
"The length of the returned array is the product of the length of `self` and the argument arrays.",
|
1692
|
+
{"(other_ary, ...){|p| block}" => Array},
|
1693
|
+
"Product will yield all combinations and return `self`.",
|
1694
|
+
<<~'RUBY'.code,
|
1695
|
+
[1,2,3].product([4,5]) #=> [[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]]
|
1696
|
+
[1,2].product([1,2]) #=> [[1,1],[1,2],[2,1],[2,2]]
|
1697
|
+
[1,2].product([3,4],[5,6]) #=> [[1,3,5],[1,3,6],[1,4,5],[1,4,6],
|
1698
|
+
# [2,3,5],[2,3,6],[2,4,5],[2,4,6]]
|
1699
|
+
[1,2].product() #=> [[1],[2]]
|
1700
|
+
[1,2].product([]) #=> []
|
1701
|
+
RUBY
|
1702
|
+
"? ruby/test/ruby/test_array.rb:test_product",
|
1703
|
+
Array[1,2,3].UT([4,5]) == Array[[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]],
|
1704
|
+
Array[1,2].UT([1,2]) == Array[[1,1],[1,2],[2,1],[2,2]],
|
1705
|
+
Array[1,2].UT([3,4],[5,6]) ==
|
1706
|
+
Array[[1,3,5],[1,3,6],[1,4,5],[1,4,6], [2,3,5],[2,3,6],[2,4,5],[2,4,6]],
|
1707
|
+
Array[1,2].UT == Array[[1],[2]],
|
1708
|
+
Array[1,2].UT([]) == Array[],
|
1709
|
+
"?? bug3394 = ruby-dev:41540",
|
1710
|
+
=begin
|
1711
|
+
acc = []
|
1712
|
+
EnvUtil.under_gc_stress{[1,2].UT([3,4,5],[6,8]){|array| acc << array}}
|
1713
|
+
acc == [[1, 3, 6], [1, 3, 8], [1, 4, 6], [1, 4, 8], [1, 5, 6], [1, 5, 8],
|
1714
|
+
[2, 3, 6], [2, 3, 8], [2, 4, 6], [2, 4, 8], [2, 5, 6], [2, 5, 8]],
|
1715
|
+
def (o = Object.new).to_ary; GC.start; [3,4] end
|
1716
|
+
[1,2].UT(*[o]*10) ==
|
1717
|
+
[1,2].product([3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4], [3,4]),
|
1718
|
+
'a = []'.setup,
|
1719
|
+
[1, 2].UT([0, 1, 2, 3, 4][1, 4]){|x| expr("a") << x}.succeed?,
|
1720
|
+
expr('a').all?{|x| assert_not_include(x, 0)}
|
1721
|
+
=end
|
1722
|
+
coda
|
1723
|
+
spec "#push",
|
1724
|
+
"See also `#pop` for the opposite effect.",
|
1725
|
+
{"(obj, ...)" => Array},
|
1726
|
+
"Append — Pushes the given object(s) on to the end of this array. This expression returns the array itself, so several appends may be chained together.",
|
1727
|
+
<<~'RUBY'.code,
|
1728
|
+
a = ["a", "b", "c"]
|
1729
|
+
a.push("d", "e", "f")
|
1730
|
+
#=> ["a", "b", "c", "d", "e", "f"]
|
1731
|
+
[1, 2, 3,].push(4).push(5)
|
1732
|
+
#=> [1, 2, 3, 4, 5]
|
1733
|
+
RUBY
|
1734
|
+
"? ruby/test/ruby/test_array.rb:test_push",
|
1735
|
+
'a = Array[1, 2, 3]'.setup,
|
1736
|
+
expr('a').UT(4, 5) == Array[1, 2, 3, 4, 5],
|
1737
|
+
expr('a').UT(nil) == Array[1, 2, 3, 4, 5, nil],
|
1738
|
+
expr('a').UT.succeed?,
|
1739
|
+
RECEIVER == Array[1, 2, 3, 4, 5, nil],
|
1740
|
+
expr('a').UT(6, 7).succeed?,
|
1741
|
+
RECEIVER == Array[1, 2, 3, 4, 5, nil, 6, 7],
|
1742
|
+
"? ruby/test/ruby/test_array.rb:test_beg_end_0:6/6",
|
1743
|
+
'x = [1, 2]'.setup,
|
1744
|
+
expr('x').UT(3, 4) == [1, 2, 3, 4],
|
1745
|
+
expr('x').UT(5) == [1, 2, 3, 4, 5],
|
1746
|
+
RECEIVER == [1, 2, 3, 4, 5],
|
1747
|
+
coda
|
1748
|
+
spec "#rassoc",
|
1749
|
+
"See also `delete_if`.",
|
1750
|
+
{"(obj)" => Array|value(nil)},
|
1751
|
+
"Searches through the array whose elements are also arrays.",
|
1752
|
+
"Compares `obj` with the second element of each contained array using `obj.==`.",
|
1753
|
+
"Returns the first contained array that matches `obj`.",
|
1754
|
+
<<~'RUBY'.code,
|
1755
|
+
a = [[1, "one"], [2, "two"], [3, "three"], ["ii", "two"]]
|
1756
|
+
a.rassoc("two") #=> [2, "two"]
|
1757
|
+
a.rassoc("four") #=> nil
|
1758
|
+
RUBY
|
1759
|
+
"? ruby/test/ruby/test_array.rb:test_rassoc",
|
1760
|
+
<<~'RUBY'.setup,
|
1761
|
+
a1 = Array[*%w(cat feline)]
|
1762
|
+
a2 = Array[*%w(dog canine)]
|
1763
|
+
a3 = Array[*%w(mule asinine)]
|
1764
|
+
a = Array[a1, a2, a3]
|
1765
|
+
RUBY
|
1766
|
+
expr('a').UT("feline") == expr('a1'),
|
1767
|
+
expr('a').UT("asinine") == expr('a3'),
|
1768
|
+
expr('a').UT("dog").nil?,
|
1769
|
+
expr('a').UT("mule").nil?,
|
1770
|
+
expr('a').UT(1..2).nil?,
|
1771
|
+
coda
|
1772
|
+
spec "#reject",
|
1773
|
+
"See also `#delete_if`",
|
1774
|
+
{"{|item| block}" => Array},
|
1775
|
+
"Returns a new array containing the items in `self` for which the given block is not true.",
|
1776
|
+
{"" => Enumerator},
|
1777
|
+
"? ruby/test/ruby/test_array.rb:test_reject",
|
1778
|
+
[0, 1, 2, 3].UT{|x| (x % 2).zero?} == [1, 3],
|
1779
|
+
coda
|
1780
|
+
spec "#reject!",
|
1781
|
+
"See also `Enumerable#reject` and `#delete_if`.",
|
1782
|
+
{"{|item| block}" => Array},
|
1783
|
+
"Equivalent to `#delete_if`, deleting elements from `self` for which the block evaluates to `true`, but returns `nil` if no changes were made.",
|
1784
|
+
"The array is changed instantly every time the block is called, not after the iteration is over.",
|
1785
|
+
{"{|item| block}" => NilClass},
|
1786
|
+
{"" => Enumerator},
|
1787
|
+
"? ruby/test/ruby/test_array.rb:test_reject!",
|
1788
|
+
Array[1, 2, 3, 4, 5].UT{false}.nil?,
|
1789
|
+
RECEIVER == Array[1, 2, 3, 4, 5],
|
1790
|
+
'a = Array[1, 2, 3, 4, 5]'.setup,
|
1791
|
+
expr('a').UT{true}== expr('a'),
|
1792
|
+
RECEIVER == Array[],
|
1793
|
+
'a = Array[1, 2, 3, 4, 5]'.setup,
|
1794
|
+
expr('a').UT{|i| i > 3} == expr('a'),
|
1795
|
+
RECEIVER == Array[1, 2, 3],
|
1796
|
+
"?? bug2545 ruby-core:27366",
|
1797
|
+
'a = Array[5, 6, 7, 8, 9, 10]'.setup,
|
1798
|
+
expr('a').UT{|i| break i if i > 8; expr('a')[0] == i || true if i < 7} == 9,
|
1799
|
+
RECEIVER == Array[7, 8, 9, 10],
|
1800
|
+
coda
|
1801
|
+
spec "#repeated_combination",
|
1802
|
+
{"(n){|c| block}" => Array},
|
1803
|
+
"When invoked with a block, yields all repeated combinations of length `n` of elements from the array and then returns the array itself.",
|
1804
|
+
"The implementation makes no guarantees about the order in which the repeated combinations are yielded.",
|
1805
|
+
{"(n)" => Enumerator},
|
1806
|
+
"If no block is given, an `Enumerator` is returned instead.",
|
1807
|
+
"Examples:",
|
1808
|
+
<<~'RUBY'.code,
|
1809
|
+
a = [1, 2, 3]
|
1810
|
+
a.repeated_combination(1).to_a #=> [[1], [2], [3]]
|
1811
|
+
a.repeated_combination(2).to_a #=> [[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]]
|
1812
|
+
a.repeated_combination(3).to_a #=> [[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],
|
1813
|
+
# [1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]]
|
1814
|
+
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],
|
1815
|
+
# [1,1,3,3],[1,2,2,2],[1,2,2,3],[1,2,3,3],[1,3,3,3],
|
1816
|
+
# [2,2,2,2],[2,2,2,3],[2,2,3,3],[2,3,3,3],[3,3,3,3]]
|
1817
|
+
a.repeated_combination(0).to_a #=> [[]] # one combination of length 0
|
1818
|
+
RUBY
|
1819
|
+
coda
|
1820
|
+
spec "#repeated_permutation",
|
1821
|
+
{"(n){|p| block}" => Array},
|
1822
|
+
"When invoked with a block, yield all repeated permutations of length `n` of the elements of the array, then return the array itself.",
|
1823
|
+
"The implementation makes no guarantees about the order in which the repeated permutations are yielded.",
|
1824
|
+
{"(n)" => Enumerator},
|
1825
|
+
"Examples:",
|
1826
|
+
<<~'RUBY'.code,
|
1827
|
+
a = [1, 2]
|
1828
|
+
a.repeated_permutation(1).to_a #=> [[1], [2]]
|
1829
|
+
a.repeated_permutation(2).to_a #=> [[1,1],[1,2],[2,1],[2,2]]
|
1830
|
+
a.repeated_permutation(3).to_a #=> [[1,1,1],[1,1,2],[1,2,1],[1,2,2],
|
1831
|
+
# [2,1,1],[2,1,2],[2,2,1],[2,2,2]]
|
1832
|
+
a.repeated_permutation(0).to_a #=> [[]] # one permutation of length 0
|
1833
|
+
RUBY
|
1834
|
+
coda
|
1835
|
+
spec "#replace",
|
1836
|
+
{"(other_ary)" => Array},
|
1837
|
+
"Replaces the contents of `self` with the contents of `other_ary`, truncating or expanding if necessary.",
|
1838
|
+
<<~'RUBY'.code,
|
1839
|
+
a = ["a", "b", "c", "d", "e"]
|
1840
|
+
a.replace(["x", "y", "z"]) #=> ["x", "y", "z"]
|
1841
|
+
a #=> ["x", "y", "z"]
|
1842
|
+
RUBY
|
1843
|
+
"? ruby/test/ruby/test_array.rb:test_replace_shared_ary",
|
1844
|
+
<<~'RUBY'.setup,
|
1845
|
+
a = [1] * 100
|
1846
|
+
b = []
|
1847
|
+
RUBY
|
1848
|
+
expr('b').UT(expr('a')),
|
1849
|
+
expr('a').UT([1, 2, 3]).succeed?,
|
1850
|
+
RECEIVER == [1, 2, 3],
|
1851
|
+
# expr('b') == [1] * 100,
|
1852
|
+
coda
|
1853
|
+
spec "#reverse",
|
1854
|
+
{"" => Array},
|
1855
|
+
"Returns a new array containing `self`‘s elements in reverse order.",
|
1856
|
+
<<~'RUBY'.code,
|
1857
|
+
["a", "b", "c"].reverse #=> ["c", "b", "a"]
|
1858
|
+
[1].reverse #=> [1]
|
1859
|
+
RUBY
|
1860
|
+
"? ruby/test/ruby/test_array.rb:test_reverse",
|
1861
|
+
Array[*%w(dog cat bee ant)].UT == Array[*%w(ant bee cat dog)],
|
1862
|
+
RECEIVER == Array[*%w(dog cat bee ant)],
|
1863
|
+
Array[].UT == Array[],
|
1864
|
+
coda
|
1865
|
+
spec "#reverse!",
|
1866
|
+
{"" => Array},
|
1867
|
+
"Reverses `self` in place.",
|
1868
|
+
<<~'RUBY'.code,
|
1869
|
+
a = ["a", "b", "c"]
|
1870
|
+
a.reverse! #=> ["c", "b", "a"]
|
1871
|
+
a #=> ["c", "b", "a"]
|
1872
|
+
RUBY
|
1873
|
+
"? ruby/test/ruby/test_array.rb:test_reverse!",
|
1874
|
+
Array[*%w(dog cat bee ant)].UT == Array[*%w(ant bee cat dog)],
|
1875
|
+
RECEIVER == Array[*%w(ant bee cat dog)],
|
1876
|
+
"?? Array#reverse always returns self.",
|
1877
|
+
Array[].UT == Array[],
|
1878
|
+
coda
|
1879
|
+
spec "#reverse_each",
|
1880
|
+
{"{|item| block}" => Array},
|
1881
|
+
{"" => Enumerator},
|
1882
|
+
"Same as `#each`, but traverses `self` in reverse order.",
|
1883
|
+
<<~'RUBY'.code,
|
1884
|
+
a = ["a", "b", "c"]
|
1885
|
+
a.reverse_each{|x| print x, " "}
|
1886
|
+
RUBY
|
1887
|
+
"produces:",
|
1888
|
+
<<~'RUBY'.code,
|
1889
|
+
c b a
|
1890
|
+
RUBY
|
1891
|
+
coda
|
1892
|
+
spec "#rindex",
|
1893
|
+
"See also `#index`.",
|
1894
|
+
{"(obj)" => Integer|value(nil)},
|
1895
|
+
"Returns the index of the last object in `self` `==` to `obj`. Returns `nil` if no match is found.",
|
1896
|
+
{"{|item| block}" => Integer|value(nil)},
|
1897
|
+
"Returns the index of the first object for which the block returns true, starting from the last object.",
|
1898
|
+
{"" => Enumerator},
|
1899
|
+
<<~'RUBY'.code,
|
1900
|
+
a = ["a", "b", "b", "b", "c"]
|
1901
|
+
a.rindex("b") #=> 3
|
1902
|
+
a.rindex("z") #=> nil
|
1903
|
+
a.rindex{|x| x == "b"} #=> 3
|
1904
|
+
RUBY
|
1905
|
+
coda
|
1906
|
+
spec "#rotate",
|
1907
|
+
{"(count=1)" => Array},
|
1908
|
+
"Returns a new array by rotating `self` so that the element at count is the first element of the new array.",
|
1909
|
+
"If count is negative then it rotates in the opposite direction, starting from the end of `self` where `-1` is the last element.",
|
1910
|
+
<<~'RUBY'.code,
|
1911
|
+
a = ["a", "b", "c", "d"]
|
1912
|
+
a.rotate #=> ["b", "c", "d", "a"]
|
1913
|
+
a #=> ["a", "b", "c", "d"]
|
1914
|
+
a.rotate(2) #=> ["c", "d", "a", "b"]
|
1915
|
+
a.rotate(-3) #=> ["b", "c", "d", "a"]
|
1916
|
+
RUBY
|
1917
|
+
"? ruby/test/ruby/test_array.rb:test_rotate",
|
1918
|
+
'a = [1,2,3,4,5]'.setup,
|
1919
|
+
expr('a').UT == [2,3,4,5,1],
|
1920
|
+
expr('a').UT(-1) == [5,1,2,3,4],
|
1921
|
+
expr('a').UT(2) == [3,4,5,1,2],
|
1922
|
+
expr('a').UT(-2) == [4,5,1,2,3],
|
1923
|
+
expr('a').UT(13) == [4,5,1,2,3],
|
1924
|
+
expr('a').UT(-13) == [3,4,5,1,2],
|
1925
|
+
'a = [1]'.setup,
|
1926
|
+
expr('a').UT == [1],
|
1927
|
+
expr('a').UT(2) == [1],
|
1928
|
+
expr('a').UT(-4) == [1],
|
1929
|
+
expr('a').UT(13) == [1],
|
1930
|
+
expr('a').UT(-13) == [1],
|
1931
|
+
'a = []'.setup,
|
1932
|
+
expr('a').UT == [],
|
1933
|
+
expr('a').UT(2) == [],
|
1934
|
+
expr('a').UT(-4) == [],
|
1935
|
+
expr('a').UT(13) == [],
|
1936
|
+
expr('a').UT(-13) == [],
|
1937
|
+
[1,2,3].UT(1, 1).raise?(ArgumentError),
|
1938
|
+
[1,2,3,4,5].UT(2**31-0.1) == [1,2,3,4,5].rotate(2**31-1),
|
1939
|
+
[1,2,3,4,5].UT(-2**31-0.9) == [1,2,3,4,5].rotate(-2**31),
|
1940
|
+
coda
|
1941
|
+
spec "#rotate!",
|
1942
|
+
{"(count=1)" => Array},
|
1943
|
+
"Rotates `self` in place so that the element at count comes first, and returns `self`.",
|
1944
|
+
"If count is negative, then it rotates in the opposite direction, starting from the end of the array where `-1` is the last element.",
|
1945
|
+
<<~'RUBY'.code,
|
1946
|
+
a = ["a", "b", "c", "d"]
|
1947
|
+
a.rotate! #=> ["b", "c", "d", "a"]
|
1948
|
+
a #=> ["b", "c", "d", "a"]
|
1949
|
+
a.rotate!(2) #=> ["d", "a", "b", "c"]
|
1950
|
+
a.rotate!(-3) #=> ["a", "b", "c", "d"]
|
1951
|
+
RUBY
|
1952
|
+
"? ruby/test/ruby/test_array.rb:test_rotate!",
|
1953
|
+
'a = [1,2,3,4,5]'.setup,
|
1954
|
+
expr('a').UT == [2,3,4,5,1],
|
1955
|
+
RECEIVER == [2,3,4,5,1],
|
1956
|
+
expr('a').UT(2) == [4,5,1,2,3],
|
1957
|
+
expr('a').UT(-4) == [5,1,2,3,4],
|
1958
|
+
expr('a').UT(13) == [3,4,5,1,2],
|
1959
|
+
expr('a').UT(-13) == [5,1,2,3,4],
|
1960
|
+
'a = [1]'.setup,
|
1961
|
+
expr('a').UT == [1],
|
1962
|
+
expr('a').UT(2) == [1],
|
1963
|
+
expr('a').UT(-4) == [1],
|
1964
|
+
expr('a').UT(13) == [1],
|
1965
|
+
expr('a').UT(-13) == [1],
|
1966
|
+
'a = []'.setup,
|
1967
|
+
expr('a').UT == [],
|
1968
|
+
expr('a').UT(2) == [],
|
1969
|
+
expr('a').UT(-4) == [],
|
1970
|
+
expr('a').UT(13) == [],
|
1971
|
+
expr('a').UT(-13) == [],
|
1972
|
+
[].UT.raise?(RuntimeError, message: /can\'t modify frozen/),
|
1973
|
+
[1,2,3].UT(1, 1).raise?(ArgumentError),
|
1974
|
+
coda
|
1975
|
+
spec "#sample",
|
1976
|
+
"The elements are chosen by using random and unique indices into the array in order to ensure that an element doesn’t repeat itself unless the array already contained duplicate elements.",
|
1977
|
+
"If the array is empty the first form returns `nil` and the second form returns an empty array.",
|
1978
|
+
{"" => Object},
|
1979
|
+
"Chooses a random element from the array. If the array is empty,returns `nil`.",
|
1980
|
+
{"(n)" => Array},
|
1981
|
+
"Chooses `n` random elements from the array. If the array is empty, returns an empty array.",
|
1982
|
+
{"(random: rng)" => Object},
|
1983
|
+
{"(n, random: rng)" => Array},
|
1984
|
+
"The optional `rng` argument will be used as the random number generator.",
|
1985
|
+
<<~'RUBY'.code,
|
1986
|
+
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
1987
|
+
a.sample #=> 7
|
1988
|
+
a.sample(4) #=> [6, 4, 2, 5]
|
1989
|
+
RUBY
|
1990
|
+
coda
|
1991
|
+
spec "#select",
|
1992
|
+
"See also `Enumerable#select`.",
|
1993
|
+
{"{|item| block}" => Array},
|
1994
|
+
"Returns a new array containing all elements of ary for which the given block returns a true value.",
|
1995
|
+
{"" => Enumerator},
|
1996
|
+
<<~'RUBY'.code,
|
1997
|
+
[1,2,3,4,5].select{|num| num.even?} #=> [2, 4]
|
1998
|
+
|
1999
|
+
a = %w{a b c d e f}
|
2000
|
+
a.select{|v| v =~ /[aeiou]/} #=> ["a", "e"]
|
2001
|
+
RUBY
|
2002
|
+
"? ruby/test/ruby/test_array.rb:test_find_all_0:1/2",
|
2003
|
+
[].UT.succeed?,
|
2004
|
+
coda
|
2005
|
+
spec "#select!",
|
2006
|
+
"See also `#keep_if`",
|
2007
|
+
{"{|item| block}" => Array|value(nil)},
|
2008
|
+
"Invokes the given block passing in successive elements from `self`, deleting elements for which the block returns a false value.",
|
2009
|
+
"If changes were made, it will return `self`, otherwise it returns `nil`.",
|
2010
|
+
{"" => Enumerator},
|
2011
|
+
coda
|
2012
|
+
spec "#shift",
|
2013
|
+
"See also `#unshift` for the opposite effect.",
|
2014
|
+
{"" => Object|value(nil)},
|
2015
|
+
"Removes the first element of `self` and returns it (shifting all other elements down by one). Returns `nil` if the array is empty.",
|
2016
|
+
{"(n)" => Array},
|
2017
|
+
"Returns an array of the first `n` elements (or less) just like `array.slice!(0, n)` does. With `ary` containing only the remainder elements, not including what was shifted to `new_ary`.",
|
2018
|
+
<<~'RUBY'.code,
|
2019
|
+
args = ["-m", "-q", "filename"]
|
2020
|
+
args.shift #=> "-m"
|
2021
|
+
args #=> ["-q", "filename"]
|
2022
|
+
|
2023
|
+
args = ["-m", "-q", "filename"]
|
2024
|
+
args.shift(2) #=> ["-m", "-q"]
|
2025
|
+
args #=> ["filename"]
|
2026
|
+
RUBY
|
2027
|
+
"? ruby/test/ruby/test_array.rb:test_unshift_error",
|
2028
|
+
[].UT("cat").raise?(RuntimeError),
|
2029
|
+
[].UT.raise?(RuntimeError),
|
2030
|
+
"? ruby/test/ruby/test_array.rb:test_beg_end_0:3/6",
|
2031
|
+
'x = [1, 2, 3, 4, 5]'.setup,
|
2032
|
+
expr('x').UT == 1,
|
2033
|
+
expr('x').UT(3) == [2, 3, 4],
|
2034
|
+
RECEIVER == [5],
|
2035
|
+
coda
|
2036
|
+
spec "#shuffle",
|
2037
|
+
{"" => Array},
|
2038
|
+
"Returns a new array with elements of `self` shuffled.",
|
2039
|
+
<<~'RUBY'.code,
|
2040
|
+
a = [1, 2, 3] #=> [1, 2, 3]
|
2041
|
+
a.shuffle #=> [2, 3, 1]
|
2042
|
+
RUBY
|
2043
|
+
{"(random: rng)" => Array},
|
2044
|
+
"The optional `rng` argument will be used as the random number generator.",
|
2045
|
+
<<~'RUBY'.code,
|
2046
|
+
a.shuffle(random: Random.new(1)) #=> [1, 3, 2]
|
2047
|
+
RUBY
|
2048
|
+
coda
|
2049
|
+
spec "#shuffle!",
|
2050
|
+
{"" => Array},
|
2051
|
+
"Shuffles elements in `self` in place.",
|
2052
|
+
{"(random: rng)" => Array},
|
2053
|
+
"The optional `rng` argument will be used as the random number generator.",
|
2054
|
+
coda
|
2055
|
+
spec "#size",
|
2056
|
+
"Alias for: `length`",
|
2057
|
+
"? ruby/test/ruby/test_array.rb:test_size",
|
2058
|
+
Array[].UT.zero?,
|
2059
|
+
Array[1].UT == 1,
|
2060
|
+
expr('Array[*(0..99).to_a]').UT == 100,
|
2061
|
+
coda
|
2062
|
+
spec "#slice",
|
2063
|
+
"Element Reference",
|
2064
|
+
"Negative indices count backward from the end of the array (`-1` is the last element).",
|
2065
|
+
"Returns `nil` if the index (or starting index) are out of range.",
|
2066
|
+
{"(index)" => Object|value(nil)},
|
2067
|
+
"Returns the element at `index`.",
|
2068
|
+
{"(start, length)" => Array|value(nil)},
|
2069
|
+
{"(range)" => Array|value(nil)},
|
2070
|
+
"Returns a subarray starting at the `start` index and continuing for `length` elements, or a subarray specified by `range` of indices.",
|
2071
|
+
"The starting index is just before an element.",
|
2072
|
+
"An empty array is returned when the starting index for an element range is at the end of the array.",
|
2073
|
+
<<~'RUBY'.code,
|
2074
|
+
a = ["a", "b", "c", "d", "e"]
|
2075
|
+
a[2] + a[0] + a[1] #=> "cab"
|
2076
|
+
a[6] #=> nil
|
2077
|
+
a[1, 2] #=> ["b", "c"]
|
2078
|
+
a[1..3] #=> ["b", "c", "d"]
|
2079
|
+
a[4..7] #=> ["e"]
|
2080
|
+
a[6..10] #=> nil
|
2081
|
+
a[-3, 3] #=> ["c", "d", "e"]
|
2082
|
+
# special cases
|
2083
|
+
a[5] #=> nil
|
2084
|
+
a[6, 1] #=> nil
|
2085
|
+
a[5, 1] #=> []
|
2086
|
+
a[5..10] #=> []
|
2087
|
+
RUBY
|
2088
|
+
coda
|
2089
|
+
spec "#slice!",
|
2090
|
+
"Returns the deleted object (or objects), or `nil` if the index is out of range.",
|
2091
|
+
{"(index)" => Object|value(nil)},
|
2092
|
+
"Deletes the element given by an index.",
|
2093
|
+
{"(start, length)" => Array|value(nil)},
|
2094
|
+
{"(range)" => Array|value(nil)},
|
2095
|
+
"Deletes the element(s) given by an index up to `length` elements or by `range`.",
|
2096
|
+
<<~'RUBY'.code,
|
2097
|
+
a = ["a", "b", "c"]
|
2098
|
+
a.slice!(1) #=> "b"
|
2099
|
+
a #=> ["a", "c"]
|
2100
|
+
a.slice!(-1) #=> "c"
|
2101
|
+
a #=> ["a"]
|
2102
|
+
a.slice!(100) #=> nil
|
2103
|
+
a #=> ["a"]
|
2104
|
+
RUBY
|
2105
|
+
"? ruby/test/ruby/test_array.rb:test_slice!",
|
2106
|
+
Array[1, 2, 3, 4, 5].UT(2) == 3,
|
2107
|
+
RECEIVER == Array[1, 2, 4, 5],
|
2108
|
+
Array[1, 2, 3, 4, 5].UT(-2) == 4,
|
2109
|
+
RECEIVER == Array[1, 2, 3, 5],
|
2110
|
+
Array[1, 2, 3, 4, 5].UT(2,2) == Array[3,4],
|
2111
|
+
RECEIVER == Array[1, 2, 5],
|
2112
|
+
Array[1, 2, 3, 4, 5].UT(-2,2) == Array[4,5],
|
2113
|
+
RECEIVER == Array[1, 2, 3],
|
2114
|
+
Array[1, 2, 3, 4, 5].UT(2..3) == Array[3,4],
|
2115
|
+
RECEIVER == Array[1, 2, 5],
|
2116
|
+
Array[1, 2, 3, 4, 5].UT(20).nil?,
|
2117
|
+
RECEIVER == Array[1, 2, 3, 4, 5],
|
2118
|
+
Array[1, 2, 3, 4, 5].UT(-6).nil?,
|
2119
|
+
RECEIVER == Array[1, 2, 3, 4, 5],
|
2120
|
+
Array[1, 2, 3, 4, 5].UT(-6..4).nil?,
|
2121
|
+
RECEIVER == Array[1, 2, 3, 4, 5],
|
2122
|
+
Array[1, 2, 3, 4, 5].UT(-6,2).nil?,
|
2123
|
+
RECEIVER == Array[1, 2, 3, 4, 5],
|
2124
|
+
Array[1].UT.raise?(ArgumentError),
|
2125
|
+
Array[1].UT(0, 0, 0).raise?(ArgumentError),
|
2126
|
+
coda
|
2127
|
+
spec "#sort",
|
2128
|
+
"See also `Enumerable#sort_by`.",
|
2129
|
+
"Returns a new array created by sorting `self`.",
|
2130
|
+
{"" => Array},
|
2131
|
+
"Comparisons for the `sort` will be done using the `<=>` operator.",
|
2132
|
+
{"{|a, b| block}" => Array},
|
2133
|
+
"Comparisons for the `sort` will be done using the code block.",
|
2134
|
+
"The block must implement a comparison between `a` and `b`, and return `-1`, when `a` follows `b`, `0` when `a` and `b` are equivalent, or `+1` if `b` follows `a`.",
|
2135
|
+
<<~'RUBY'.code,
|
2136
|
+
a = ["d", "a", "e", "c", "b"]
|
2137
|
+
a.sort #=> ["a", "b", "c", "d", "e"]
|
2138
|
+
a.sort{|x,y| y <=> x} #=> ["e", "d", "c", "b", "a"]
|
2139
|
+
RUBY
|
2140
|
+
"? ruby/test/ruby/test_array.rb:test_sort_0:1/2",
|
2141
|
+
["it", "came", "to", "pass", "that", "..."].UT
|
2142
|
+
.join(" ") == "... came it pass that to",
|
2143
|
+
coda
|
2144
|
+
spec "#sort!",
|
2145
|
+
"See also `Enumerable#sort_by`.",
|
2146
|
+
"Sorts `self` in place.",
|
2147
|
+
{"" => Array},
|
2148
|
+
"Comparisons for the `sort` will be done using the `<=>` operator.",
|
2149
|
+
{"{|a, b| block}" => Array},
|
2150
|
+
"Comparisons for the `sort` will be done using the code block.",
|
2151
|
+
"The block must implement a comparison between `a` and `b`, and return `-1`, when `a` follows `b`, `0` when `a` and `b` are equivalent, or `+1` if `b` follows `a`.",
|
2152
|
+
<<~'RUBY'.code,
|
2153
|
+
a = ["d", "a", "e", "c", "b"]
|
2154
|
+
a.sort! #=> ["a", "b", "c", "d", "e"]
|
2155
|
+
a.sort!{|x,y| y <=> x} #=> ["e", "d", "c", "b", "a"]
|
2156
|
+
RUBY
|
2157
|
+
"? ruby/test/ruby/test_array.rb:test_sort_0:2/2",
|
2158
|
+
"?? sort with condition",
|
2159
|
+
[2,5,3,1,7].UT{|a,b| a<=>b}.succeed?,
|
2160
|
+
RECEIVER == [1,2,3,5,7],
|
2161
|
+
"?? reverse sort",
|
2162
|
+
[2,5,3,1,7].UT{|a,b| b-a}.succeed?,
|
2163
|
+
RECEIVER == [7,5,3,2,1],
|
2164
|
+
coda
|
2165
|
+
spec "#sort_by!",
|
2166
|
+
{"{|obj| block}" => Array},
|
2167
|
+
"Sorts `self` in place using a set of keys generated by mapping the values in self through the given block.",
|
2168
|
+
{"" => Enumerator},
|
2169
|
+
"? ruby/test/ruby/test_array.rb:test_sort_by!",
|
2170
|
+
[1,3,5,2,4].UT{|x| -x}.succeed?,
|
2171
|
+
RECEIVER == [5,4,3,2,1],
|
2172
|
+
coda
|
2173
|
+
spec "#take",
|
2174
|
+
"See also `#drop`",
|
2175
|
+
{"(n)" => Array},
|
2176
|
+
"Returns first `n` elements from the array.",
|
2177
|
+
"If a negative number is given, raises an `ArgumentError`.",
|
2178
|
+
<<~'RUBY'.code,
|
2179
|
+
a = [1, 2, 3, 4, 5, 0]
|
2180
|
+
a.take(3) #=> [1, 2, 3]
|
2181
|
+
RUBY
|
2182
|
+
"? ruby/test/ruby/test_array.rb:test_take",
|
2183
|
+
[1,2,3,4,5,0].UT(3) == [1,2,3],
|
2184
|
+
"?? ruby-dev:34123",
|
2185
|
+
[1,2].UT(-1).raise?(ArgumentError),
|
2186
|
+
"?? ruby-dev:34123",
|
2187
|
+
[1,2].UT(1000000000) == [1,2],
|
2188
|
+
coda
|
2189
|
+
spec "#take_while",
|
2190
|
+
"See also `#drop_while`",
|
2191
|
+
{"{|arr| block}" => Array},
|
2192
|
+
"Passes elements to the block until the block returns `nil` or `false`, then stops iterating and returns an array of all prior elements.",
|
2193
|
+
{"" => Enumerator},
|
2194
|
+
<<~'RUBY'.code,
|
2195
|
+
a = [1, 2, 3, 4, 5, 0]
|
2196
|
+
a.take_while{|i| i < 3} #=> [1, 2]
|
2197
|
+
RUBY
|
2198
|
+
"? ruby/test/ruby/test_array.rb:test_take_while",
|
2199
|
+
[1,2,3,4,5,0].UT{|i| i < 3} == [1,2],
|
2200
|
+
coda
|
2201
|
+
spec "#to_a",
|
2202
|
+
{"" => Array},
|
2203
|
+
"Returns `self`.",
|
2204
|
+
"If called on a subclass of `Array`, converts the receiver to an `Array` object.",
|
2205
|
+
coda
|
2206
|
+
spec "#to_ary",
|
2207
|
+
{"" => Array},
|
2208
|
+
"Returns `self`.",
|
2209
|
+
coda
|
2210
|
+
spec "#to_h",
|
2211
|
+
{"" => Hash},
|
2212
|
+
"Returns the result of interpreting `ary` as an array of `[key, value]` pairs.",
|
2213
|
+
<<~'RUBY'.code,
|
2214
|
+
[[:foo, :bar], [1, 2]].to_h
|
2215
|
+
# => {:foo => :bar, 1 => 2}
|
2216
|
+
RUBY
|
2217
|
+
coda
|
2218
|
+
spec "#to_s",
|
2219
|
+
"Alias for: `inspect`.",
|
2220
|
+
"? ruby/test/ruby/test_array.rb:test_to_s",
|
2221
|
+
'$, = ""'.setup,
|
2222
|
+
Array[].UT == "[]",
|
2223
|
+
'$, = ""'.setup,
|
2224
|
+
Array[1, 2].UT == "[1, 2]",
|
2225
|
+
'$, = ""'.setup,
|
2226
|
+
Array[1, 2, 3].UT == "[1, 2, 3]",
|
2227
|
+
'$, = ":"'.setup,
|
2228
|
+
Array[1, 2, 3].UT == "[1, 2, 3]",
|
2229
|
+
'$, = nil'.setup,
|
2230
|
+
teardown,
|
2231
|
+
coda
|
2232
|
+
spec "#transpose",
|
2233
|
+
{"" => Array},
|
2234
|
+
"Assumes that `self` is an array of arrays and transposes the rows and columns.",
|
2235
|
+
<<~'RUBY'.code,
|
2236
|
+
a = [[1,2], [3,4], [5,6]]
|
2237
|
+
a.transpose #=> [[1, 3, 5], [2, 4, 6]]
|
2238
|
+
RUBY
|
2239
|
+
"If the length of the subarrays don’t match, an `IndexError` is raised.",
|
2240
|
+
"? ruby/test/ruby/test_array.rb:test_transpose",
|
2241
|
+
[[1, 2, 3], [:a, :b, :c]].UT == [[1, :a], [2, :b], [3, :c]],
|
2242
|
+
[[1, 2, 3], [:a, :b]].UT.raise?(IndexError),
|
2243
|
+
coda
|
2244
|
+
spec "#uniq",
|
2245
|
+
{"" => Array},
|
2246
|
+
"Returns a new array by removing duplicate values in `self`.",
|
2247
|
+
"It compares values using their hash and eql? methods for efficiency.",
|
2248
|
+
{"{|item| ...}" => Array},
|
2249
|
+
"It will use the return value of the block for comparison.",
|
2250
|
+
<<~'RUBY'.code,
|
2251
|
+
a = ["a", "a", "b", "b", "c"]
|
2252
|
+
a.uniq # => ["a", "b", "c"]
|
2253
|
+
|
2254
|
+
b = [["student","sam"], ["student","george"], ["teacher","matz"]]
|
2255
|
+
b.uniq{|s| s.first} # => [["student", "sam"], ["teacher", "matz"]]
|
2256
|
+
RUBY
|
2257
|
+
coda
|
2258
|
+
spec "#uniq!",
|
2259
|
+
{"" => Array|value(nil)},
|
2260
|
+
"Removes duplicate elements from `self`.",
|
2261
|
+
"It compares values using their `hash` and `eql?` methods for efficiency.",
|
2262
|
+
"Returns `nil` if no changes are made (that is, no duplicates are found).",
|
2263
|
+
{"{|item| ...}" => Array|value(nil)},
|
2264
|
+
"It will use the return value of the block for comparison.",
|
2265
|
+
<<~'RUBY'.code,
|
2266
|
+
a = ["a", "a", "b", "b", "c"]
|
2267
|
+
a.uniq! # => ["a", "b", "c"]
|
2268
|
+
|
2269
|
+
b = ["a", "b", "c"]
|
2270
|
+
b.uniq! # => nil
|
2271
|
+
|
2272
|
+
c = [["student","sam"], ["student","george"], ["teacher","matz"]]
|
2273
|
+
c.uniq!{|s| s.first} # => [["student", "sam"], ["teacher", "matz"]]
|
2274
|
+
RUBY
|
2275
|
+
"? ruby/test/ruby/test_array.rb:test_uniq_0",
|
2276
|
+
[1, 1, 4, 2, 5, 4, 5, 1, 2].UT.succeed?,
|
2277
|
+
RECEIVER == [1, 4, 2, 5],
|
2278
|
+
coda
|
2279
|
+
spec "#unshift",
|
2280
|
+
"See also `#shift` for the opposite effect.",
|
2281
|
+
{"(obj, ...)" => Array},
|
2282
|
+
"Prepends objects to the front of `self`, moving other elements upwards.",
|
2283
|
+
<<~'RUBY'.code,
|
2284
|
+
a = ["b", "c", "d"]
|
2285
|
+
a.unshift("a") #=> ["a", "b", "c", "d"]
|
2286
|
+
a.unshift(1, 2) #=> [1, 2, "a", "b", "c", "d"]
|
2287
|
+
RUBY
|
2288
|
+
"? ruby/test/ruby/test_array.rb:test_beg_end_0:4/6",
|
2289
|
+
'x = [5]'.setup,
|
2290
|
+
expr('x').UT(2, 3, 4) == [2, 3, 4, 5],
|
2291
|
+
expr('x').UT(1) == [1, 2, 3, 4, 5],
|
2292
|
+
RECEIVER == [1, 2, 3, 4, 5],
|
2293
|
+
coda
|
2294
|
+
spec "#values_at",
|
2295
|
+
"See also `#select`.",
|
2296
|
+
{"(selector, ...)" => Array},
|
2297
|
+
"Returns an array containing the elements in `self` corresponding to the given selector(s).",
|
2298
|
+
"The selectors may be either integer indices or ranges.",
|
2299
|
+
<<~'RUBY'.code,
|
2300
|
+
a = %w{a b c d e f}
|
2301
|
+
a.values_at(1, 3, 5) # => ["b", "d", "f"]
|
2302
|
+
a.values_at(1, 3, 5, 7) # => ["b", "d", "f", nil]
|
2303
|
+
a.values_at(-1, -2, -2, -7) # => ["f", "e", "e", nil]
|
2304
|
+
a.values_at(4..6, 3...6) # => ["e", "f", nil, "d", "e", "f"]
|
2305
|
+
RUBY
|
2306
|
+
"? ruby/test/ruby/test_array.rb:test_values_at",
|
2307
|
+
'a = Array[*("a".."j").to_a]'.setup,
|
2308
|
+
expr('a').UT(0, 2, 4) == Array["a", "c", "e"],
|
2309
|
+
expr('a').UT(-1, -3, -5) == Array["j", "h", "f"],
|
2310
|
+
expr('a').UT(-3, 99, 0) == Array["h", nil, "a"],
|
2311
|
+
coda
|
2312
|
+
spec "#zip",
|
2313
|
+
"Converts any arguments to arrays, then merges elements of self with corresponding elements from each argument.",
|
2314
|
+
"This generates a sequence of `ary.size` n-element arrays, where n is one more than the count of arguments.",
|
2315
|
+
"If the size of any argument is less than the size of the initial array, `nil` values are supplied.",
|
2316
|
+
{"(arg, ...)" => Array},
|
2317
|
+
"An array of arrays is returned.",
|
2318
|
+
{"(arg, ...){|arr| block}" => NilClass},
|
2319
|
+
"It is invoked for each output array.",
|
2320
|
+
<<~'RUBY'.code,
|
2321
|
+
a = [4, 5, 6]
|
2322
|
+
b = [7, 8, 9]
|
2323
|
+
[1, 2, 3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
|
2324
|
+
[1, 2].zip(a, b) #=> [[1, 4, 7], [2, 5, 8]]
|
2325
|
+
a.zip([1, 2], [8]) #=> [[4, 1, 8], [5, 2, nil], [6, nil, nil]]
|
2326
|
+
RUBY
|
2327
|
+
coda
|
2328
|
+
spec "#|",
|
2329
|
+
"See also `#uniq`.",
|
2330
|
+
{"other_ary" => Array},
|
2331
|
+
"Set Union — Returns a new array by joining `ary` with `other_ary`, excluding any duplicates and preserving the order from the original array.",
|
2332
|
+
"It compares elements using their `hash` and `eql?` methods for efficiency.",
|
2333
|
+
<<~'RUBY'.code,
|
2334
|
+
["a", "b", "c"] | ["c", "d", "a"] #=> ["a", "b", "c", "d"]
|
2335
|
+
RUBY
|
2336
|
+
"? ruby/test/ruby/test_array.rb:ruby/test/ruby/test_array_andor_0:2/2",
|
2337
|
+
[1,2,3].UT([2,4,6]) == [1,2,3,4,6],
|
2338
|
+
"? ruby/test/ruby/test_array.rb:test_0_literal:5/8",
|
2339
|
+
[1,2,3].UT([2,3,4]) == [1,2,3,4],
|
2340
|
+
coda
|
2341
|
+
end
|
2342
|
+
|
2343
|
+
class String
|
2344
|
+
spec "#split",
|
2345
|
+
"? ruby/test/ruby/test_array.rb:test_split_0",
|
2346
|
+
<<~'RUBY'.setup,
|
2347
|
+
x = "The Book of Mormon"
|
2348
|
+
y = x.reverse
|
2349
|
+
RUBY
|
2350
|
+
expr('x').UT(//).reverse!.join == expr('x').reverse,
|
2351
|
+
RECEIVER.reverse! == expr('x'),
|
2352
|
+
"1 byte string".UT(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1",
|
2353
|
+
'x = "a b c d"'.setup,
|
2354
|
+
expr('x').UT == ["a", "b", "c", "d"],
|
2355
|
+
expr('x').UT(" ") == ["a", "b", "c", "d"],
|
2356
|
+
coda
|
2357
|
+
end
|
2358
|
+
|
2359
|
+
=begin
|
2360
|
+
|
2361
|
+
"? ruby/test/ruby/test_array.rb:test_percent_i",
|
2362
|
+
%i[foo bar] == [:foo, :bar],
|
2363
|
+
%i["foo] == [:"\"foo"],
|
2364
|
+
|
2365
|
+
"? ruby/test/ruby/test_array.rb:test_percent_I",
|
2366
|
+
x = 10
|
2367
|
+
%I[foo b#{x}] == [:foo, :b10],
|
2368
|
+
%I["foo#{x}] == [:"\"foo10"],
|
2369
|
+
|
2370
|
+
"? ruby/test/ruby/test_array.rb:test_misc_0",
|
2371
|
+
assert(defined? "a".chomp, '"a".chomp is not defined')
|
2372
|
+
"abc".scan(/./) == ["a", "b", "c"],
|
2373
|
+
"1a2b3c".scan(/(\d.)/) == [["1a"], ["2b"], ["3c"]],
|
2374
|
+
"?? non-greedy match",
|
2375
|
+
"a=12;b=22".scan(/(.*?)=(\d*);?/)) == [["a", "12"], ["b", "22"]],
|
2376
|
+
x = [1]
|
2377
|
+
(x * 5).join(":") == "1:1:1:1:1",
|
2378
|
+
(x * 1).join(":") == "1",
|
2379
|
+
(x * 0).join(":") == "",
|
2380
|
+
*x = *(1..7).to_a
|
2381
|
+
x.size == 7,
|
2382
|
+
x == [1, 2, 3, 4, 5, 6, 7],
|
2383
|
+
x = [1,2,3]
|
2384
|
+
x[1,0] = x
|
2385
|
+
x == [1,1,2,3,2,3],
|
2386
|
+
x = [1,2,3]
|
2387
|
+
x[-1,0] = x
|
2388
|
+
x == [1,2,1,2,3,3],
|
2389
|
+
x = [1,2,3]
|
2390
|
+
x.concat(x)
|
2391
|
+
x == [1,2,3,1,2,3],
|
2392
|
+
x = [1,2,3]
|
2393
|
+
x.clear
|
2394
|
+
x == [],
|
2395
|
+
x = [1,2,3]
|
2396
|
+
y = x.dup
|
2397
|
+
x << 4
|
2398
|
+
y << 5
|
2399
|
+
x == [1,2,3,4],
|
2400
|
+
y == [1,2,3,5]
|
2401
|
+
|
2402
|
+
"? ruby/test/ruby/test_array.rb:test_find_all_0:2/2",
|
2403
|
+
assert_respond_to([], :find_all
|
2404
|
+
[].find_all{|obj| obj == "foo"} == [],
|
2405
|
+
x = ["foo", "bar", "baz", "baz", 1, 2, 3, 3, 4]
|
2406
|
+
x.find_all{|obj| obj == "baz"} == ["baz","baz"],
|
2407
|
+
x.find_all{|obj| obj == 3} == [3,3]
|
2408
|
+
|
2409
|
+
"? ruby/test/ruby/test_array.rb:test_clone",
|
2410
|
+
for taint in [false, true]
|
2411
|
+
for frozen in [false, true]
|
2412
|
+
a = Array[*(0..99).to_a]
|
2413
|
+
a.taint if taint
|
2414
|
+
a if frozen
|
2415
|
+
b = a.clone
|
2416
|
+
b == a
|
2417
|
+
b.__id__ != a.__id__
|
2418
|
+
b.frozen? == a.frozen?
|
2419
|
+
b.tainted? == a.tainted?,
|
2420
|
+
|
2421
|
+
"? ruby/test/ruby/test_array.rb:test_count",
|
2422
|
+
a = Array[1, 2, 3, 1, 2]
|
2423
|
+
a.count == 5,
|
2424
|
+
a.count(1) == 2,
|
2425
|
+
a.count{|x| x % 2 == 1} == 3,
|
2426
|
+
a.count(1){|x| x % 2 == 1} == 2,
|
2427
|
+
a.count(0, 1).raise?(ArgumentError)
|
2428
|
+
"?? bug8654 ruby-core:56072",
|
2429
|
+
assert_in_out_err [], <<~EOS, ["0"], [],
|
2430
|
+
a1 = []
|
2431
|
+
a2 = Array.new(100){|i| i}
|
2432
|
+
a2.count do
|
2433
|
+
|i|
|
2434
|
+
p i
|
2435
|
+
a2.replace(a1) if i.zero?
|
2436
|
+
EOS
|
2437
|
+
assert_in_out_err [], <<~EOS, ["[]", "0"], [],
|
2438
|
+
ARY = Array.new(100){|i| i}
|
2439
|
+
class Fixnum
|
2440
|
+
alias old_equal == #Use `prepend`
|
2441
|
+
def == other
|
2442
|
+
ARY.replace([]) if self.equal?(0)
|
2443
|
+
p ARY
|
2444
|
+
self.equal?(other)
|
2445
|
+
p ARY.count(42)
|
2446
|
+
EOS
|
2447
|
+
|
2448
|
+
"? ruby/test/ruby/test_array.rb:test_dup",
|
2449
|
+
for taint in [false, true]
|
2450
|
+
for frozen in [false, true]
|
2451
|
+
a = Array[*(0..99).to_a]
|
2452
|
+
a.taint if taint
|
2453
|
+
a if frozen
|
2454
|
+
b = a.dup
|
2455
|
+
b == a,
|
2456
|
+
b.__id__ != a.__id__,
|
2457
|
+
b.frozen? == false,
|
2458
|
+
b.tainted? == a.tainted?,
|
2459
|
+
end
|
2460
|
+
end
|
2461
|
+
|
2462
|
+
"? ruby/test/ruby/test_array.rb:test_each",
|
2463
|
+
a = Array[*%w(ant bat cat dog)]
|
2464
|
+
i = 0
|
2465
|
+
a.each{|e|
|
2466
|
+
e == a[i]
|
2467
|
+
i += 1
|
2468
|
+
}
|
2469
|
+
i == 4,
|
2470
|
+
a = Array[]
|
2471
|
+
i = 0
|
2472
|
+
a.each{|e|
|
2473
|
+
e == a[i],
|
2474
|
+
i += 1
|
2475
|
+
}
|
2476
|
+
i.zero?,
|
2477
|
+
a.each{} == a
|
2478
|
+
|
2479
|
+
"? ruby/test/ruby/test_array.rb:test_each_index",
|
2480
|
+
a = Array[*%w(ant bat cat dog)]
|
2481
|
+
i = 0
|
2482
|
+
a.each_index{|ind|
|
2483
|
+
ind == i
|
2484
|
+
i += 1
|
2485
|
+
}
|
2486
|
+
i == 4,
|
2487
|
+
a = Array[]
|
2488
|
+
i = 0
|
2489
|
+
a.each_index{|ind|
|
2490
|
+
ind == i,
|
2491
|
+
i += 1
|
2492
|
+
}
|
2493
|
+
i.zero?,
|
2494
|
+
a.each_index{} == a,
|
2495
|
+
|
2496
|
+
"? ruby/test/ruby/test_array.rb:test_flatten_with_callcc",
|
2497
|
+
respond_to?(:callcc, true) or require "continuation"
|
2498
|
+
o = Object.new
|
2499
|
+
def o.to_ary() callcc{|k| @cont = k; [1,2,3]} end
|
2500
|
+
begin
|
2501
|
+
[10, 20, o, 30, o, 40].flatten == [10, 20, 1, 2, 3, 30, 1, 2, 3, 40],
|
2502
|
+
rescue => e
|
2503
|
+
else
|
2504
|
+
o.instance_eval{@cont}.call
|
2505
|
+
end
|
2506
|
+
"?? ruby-dev:34798",
|
2507
|
+
e.instance_of?(RuntimeError)
|
2508
|
+
e.message =~ /reentered/
|
2509
|
+
|
2510
|
+
"? ruby/test/ruby/test_array.rb:test_permutation_with_callcc",
|
2511
|
+
respond_to?(:callcc, true) or require "continuation"
|
2512
|
+
n = 1000
|
2513
|
+
cont = nil
|
2514
|
+
ary = [1,2,3]
|
2515
|
+
begin
|
2516
|
+
ary.permutation{
|
2517
|
+
callcc{|k| cont = k} unless cont
|
2518
|
+
}
|
2519
|
+
rescue => e
|
2520
|
+
end
|
2521
|
+
n -= 1
|
2522
|
+
cont.call if 0 < n
|
2523
|
+
e.instance_of?(RuntimeError),
|
2524
|
+
e.message =~ /reentered/
|
2525
|
+
|
2526
|
+
"? ruby/test/ruby/test_array.rb:test_product_with_callcc",
|
2527
|
+
respond_to?(:callcc, true) or require "continuation"
|
2528
|
+
n = 1000
|
2529
|
+
cont = nil
|
2530
|
+
ary = [1,2,3]
|
2531
|
+
begin
|
2532
|
+
ary.product{
|
2533
|
+
callcc{|k| cont = k} unless cont
|
2534
|
+
}
|
2535
|
+
rescue => e
|
2536
|
+
end
|
2537
|
+
n -= 1
|
2538
|
+
cont.call if 0 < n
|
2539
|
+
e.instance_of?(RuntimeError),
|
2540
|
+
e.message =~ /reentered/
|
2541
|
+
|
2542
|
+
"? ruby/test/ruby/test_array.rb:test_combination_with_callcc",
|
2543
|
+
respond_to?(:callcc, true) or require "continuation"
|
2544
|
+
n = 1000
|
2545
|
+
cont = nil
|
2546
|
+
ary = [1,2,3]
|
2547
|
+
begin
|
2548
|
+
ary.combination(2){
|
2549
|
+
callcc{|k| cont = k} unless cont
|
2550
|
+
}
|
2551
|
+
rescue => e
|
2552
|
+
end
|
2553
|
+
n -= 1
|
2554
|
+
cont.call if 0 < n
|
2555
|
+
e.instance_of?(RuntimeError),
|
2556
|
+
e.message =~ /reentered/
|
2557
|
+
|
2558
|
+
"? ruby/test/ruby/test_array.rb:test_repeated_permutation_with_callcc",
|
2559
|
+
respond_to?(:callcc, true) or require "continuation"
|
2560
|
+
n = 1000
|
2561
|
+
cont = nil
|
2562
|
+
ary = [1,2,3]
|
2563
|
+
begin
|
2564
|
+
ary.repeated_permutation(2){
|
2565
|
+
callcc{|k| cont = k} unless cont
|
2566
|
+
}
|
2567
|
+
rescue => e
|
2568
|
+
end
|
2569
|
+
n -= 1
|
2570
|
+
cont.call if 0 < n
|
2571
|
+
assert_instance_of?(RuntimeError, e)
|
2572
|
+
assert_match(/reentered/, e.message)
|
2573
|
+
|
2574
|
+
"? ruby/test/ruby/test_array.rb:test_repeated_combination_with_callcc",
|
2575
|
+
respond_to?(:callcc, true) or require "continuation"
|
2576
|
+
n = 1000
|
2577
|
+
cont = nil
|
2578
|
+
ary = [1,2,3]
|
2579
|
+
begin
|
2580
|
+
ary.repeated_combination(2){
|
2581
|
+
callcc{|k| cont = k} unless cont
|
2582
|
+
}
|
2583
|
+
rescue => e
|
2584
|
+
end
|
2585
|
+
n -= 1
|
2586
|
+
cont.call if 0 < n
|
2587
|
+
assert_instance_of?(RuntimeError, e)
|
2588
|
+
assert_match(/reentered/, e.message)
|
2589
|
+
|
2590
|
+
"? ruby/test/ruby/test_array.rb:test_replace",
|
2591
|
+
a = Array[1, 2, 3]
|
2592
|
+
a_id = a.__id__
|
2593
|
+
== Array[4, 5, 6], a.replace(Array[4, 5, 6]
|
2594
|
+
== Array[4, 5, 6], a
|
2595
|
+
== a_id, a.__id__
|
2596
|
+
== Array[], a.replace(Array[])
|
2597
|
+
fa = a.dup
|
2598
|
+
assert_nothing_raised(RuntimeError){a.replace(a)}
|
2599
|
+
fa.replace(fa).raise?(RuntimeError)
|
2600
|
+
fa.replace.raise?(ArgumentError)
|
2601
|
+
a.replace(42).raise?(TypeError)
|
2602
|
+
fa.replace(42).raise?(RuntimeError)
|
2603
|
+
|
2604
|
+
"? ruby/test/ruby/test_array.rb:test_reverse_each",
|
2605
|
+
a = Array[*%w(dog cat bee ant)]
|
2606
|
+
i = a.length
|
2607
|
+
a.reverse_each{|e|
|
2608
|
+
i -= 1
|
2609
|
+
== a[i], e
|
2610
|
+
}
|
2611
|
+
== 0, i
|
2612
|
+
a = Array[]
|
2613
|
+
i = 0
|
2614
|
+
a.reverse_each{|e|
|
2615
|
+
i += 1
|
2616
|
+
assert(false, "Never get here")
|
2617
|
+
}
|
2618
|
+
== 0, i
|
2619
|
+
|
2620
|
+
"? ruby/test/ruby/test_array.rb:test_rindex",
|
2621
|
+
a = Array["cat", 99, /a/, 99, [1, 2, 3]]
|
2622
|
+
== 0, a.rindex("cat")
|
2623
|
+
== 3, a.rindex(99)
|
2624
|
+
== 4, a.rindex([1,2,3])
|
2625
|
+
assert_nil(a.rindex("ca")
|
2626
|
+
assert_nil(a.rindex([1,2])
|
2627
|
+
== 3, a.rindex(99){|x| x == [1,2,3]}
|
2628
|
+
|
2629
|
+
"? ruby/test/ruby/test_array.rb:test_shift",
|
2630
|
+
a = Array["cat", "dog"]
|
2631
|
+
== "cat", a.shift
|
2632
|
+
== Array["dog"], a
|
2633
|
+
== "dog", a.shift
|
2634
|
+
== Array[], a
|
2635
|
+
assert_nil(a.shift)
|
2636
|
+
== Array[], a
|
2637
|
+
|
2638
|
+
"? ruby/test/ruby/test_array.rb:test_slice",
|
2639
|
+
a = Array[*(1..100).to_a]
|
2640
|
+
== 1, a.slice(0)
|
2641
|
+
== 100, a.slice(99)
|
2642
|
+
assert_nil(a.slice(100)
|
2643
|
+
== 100, a.slice(-1)
|
2644
|
+
== 99, a.slice(-2)
|
2645
|
+
== 1, a.slice(-100)
|
2646
|
+
assert_nil(a.slice(-101)
|
2647
|
+
== Array[1], a.slice(0,1)
|
2648
|
+
== Array[100], a.slice(99,1)
|
2649
|
+
== Array[], a.slice(100,1)
|
2650
|
+
== Array[100], a.slice(99,100)
|
2651
|
+
== Array[100], a.slice(-1,1)
|
2652
|
+
== Array[99], a.slice(-2,1)
|
2653
|
+
== Array[10, 11, 12], a.slice(9, 3)
|
2654
|
+
== Array[10, 11, 12], a.slice(-91, 3)
|
2655
|
+
assert_nil(a.slice(-101, 2)
|
2656
|
+
== Array[1], a.slice(0..0)
|
2657
|
+
== Array[100], a.slice(99..99)
|
2658
|
+
== Array[], a.slice(100..100)
|
2659
|
+
== Array[100], a.slice(99..200)
|
2660
|
+
== Array[100], a.slice(-1..-1)
|
2661
|
+
== Array[99], a.slice(-2..-2)
|
2662
|
+
== Array[10, 11, 12], a.slice(9..11)
|
2663
|
+
== Array[10, 11, 12], a.slice(-91..-89)
|
2664
|
+
assert_nil(a.slice(-101..-1)
|
2665
|
+
assert_nil(a.slice(10, -3)
|
2666
|
+
# Ruby 1.8 feature change:
|
2667
|
+
# Array#slice[size..x] always returns [].
|
2668
|
+
#assert_nil(a.slice(10..7)
|
2669
|
+
assert_equal Array[], a.slice(10..7)
|
2670
|
+
|
2671
|
+
"? ruby/test/ruby/test_array.rb:test_sort",
|
2672
|
+
a = Array[4, 1, 2, 3]
|
2673
|
+
== Array[1, 2, 3, 4], a.sort
|
2674
|
+
== Array[4, 1, 2, 3], a
|
2675
|
+
== Array[4, 3, 2, 1], a.sort{|x, y| y <=> x}
|
2676
|
+
== Array[4, 1, 2, 3], a
|
2677
|
+
== Array[1, 2, 3, 4], a.sort{|x, y| (x - y) * (2**100)}
|
2678
|
+
a.fill(1)
|
2679
|
+
== Array[1, 1, 1, 1], a.sort
|
2680
|
+
== Array[], Array[].sort
|
2681
|
+
|
2682
|
+
"? ruby/test/ruby/test_array.rb:test_sort!",
|
2683
|
+
a = Array[4, 1, 2, 3]
|
2684
|
+
== Array[1, 2, 3, 4], a.sort!
|
2685
|
+
== Array[1, 2, 3, 4], a
|
2686
|
+
== Array[4, 3, 2, 1], a.sort!{|x, y| y <=> x}
|
2687
|
+
== Array[4, 3, 2, 1], a
|
2688
|
+
a.fill(1)
|
2689
|
+
== Array[1, 1, 1, 1], a.sort!
|
2690
|
+
== Array[1], Array[1].sort!
|
2691
|
+
== Array[], Array[].sort!
|
2692
|
+
a = Array[4, 3, 2, 1]
|
2693
|
+
a.sort!{|m, n| a.replace([9, 8, 7, 6]); m <=> n}
|
2694
|
+
== [1, 2, 3, 4], a
|
2695
|
+
a = Array[4, 3, 2, 1]
|
2696
|
+
a.sort!{|m, n| a.replace([9, 8, 7]); m <=> n}
|
2697
|
+
== [1, 2, 3, 4], a
|
2698
|
+
|
2699
|
+
"? ruby/test/ruby/test_array.rb:test_sort_with_callcc",
|
2700
|
+
respond_to?(:callcc, true) or require "continuation"
|
2701
|
+
n = 1000
|
2702
|
+
cont = nil
|
2703
|
+
ary = (1..100).to_a
|
2704
|
+
begin
|
2705
|
+
ary.sort!{|a,b|
|
2706
|
+
callcc{|k| cont = k} unless cont
|
2707
|
+
== 100, ary.size, "ruby-core:16679"
|
2708
|
+
a <=> b
|
2709
|
+
}
|
2710
|
+
rescue => e
|
2711
|
+
end
|
2712
|
+
n -= 1
|
2713
|
+
cont.call if 0 < n
|
2714
|
+
assert_instance_of?(RuntimeError, e, "ruby-core:16679")
|
2715
|
+
assert_match(/reentered/, e.message, "ruby-core:16679")
|
2716
|
+
|
2717
|
+
"? ruby/test/ruby/test_array.rb:test_sort_with_replace",
|
2718
|
+
xary = (1..100).to_a
|
2719
|
+
100.times do
|
2720
|
+
ary = (1..100).to_a
|
2721
|
+
ary.sort!{|a,b| ary.replace(xary); a <=> b}
|
2722
|
+
GC.start
|
2723
|
+
== xary, ary, "ruby-dev:34732"
|
2724
|
+
end
|
2725
|
+
|
2726
|
+
"? ruby/test/ruby/test_array.rb:test_sort_bang_with_freeze",
|
2727
|
+
ary = []
|
2728
|
+
o1 = Object.new
|
2729
|
+
o1.singleton_class.class_eval{
|
2730
|
+
define_method(:<=>){|v|
|
2731
|
+
ary
|
2732
|
+
1
|
2733
|
+
}
|
2734
|
+
}
|
2735
|
+
o2 = o1.dup
|
2736
|
+
ary << o1 << o2
|
2737
|
+
orig = ary.dup
|
2738
|
+
ary.sort!.raise?(RuntimeError, message: "frozen during comparison")
|
2739
|
+
== orig, ary, "must not be modified once frozen"
|
2740
|
+
|
2741
|
+
"? ruby/test/ruby/test_array.rb:test_to_a",
|
2742
|
+
a = Array[1, 2, 3]
|
2743
|
+
a_id = a.__id__
|
2744
|
+
== a, a.to_a
|
2745
|
+
== a_id, a.to_a.__id__
|
2746
|
+
|
2747
|
+
"? ruby/test/ruby/test_array.rb:test_to_ary",
|
2748
|
+
a = [1, 2, 3]
|
2749
|
+
b = Array[*a]
|
2750
|
+
a_id = a.__id__
|
2751
|
+
== a, b.to_ary
|
2752
|
+
if (Array == Array)
|
2753
|
+
== a_id, a.to_ary.__id__
|
2754
|
+
end
|
2755
|
+
o = Object.new
|
2756
|
+
def o.to_ary
|
2757
|
+
[4, 5]
|
2758
|
+
end
|
2759
|
+
== [1, 2, 3, 4, 5], a.concat(o)
|
2760
|
+
o = Object.new
|
2761
|
+
def o.to_ary
|
2762
|
+
foo_bar()
|
2763
|
+
end
|
2764
|
+
assert_match(/foo_bar/, assert_raise(NoMethodError){a.concat(o)}.message)
|
2765
|
+
|
2766
|
+
"? ruby/test/ruby/test_array.rb:test_to_h",
|
2767
|
+
kvp = Object.new
|
2768
|
+
def kvp.to_ary
|
2769
|
+
[:obtained, :via_to_ary]
|
2770
|
+
end
|
2771
|
+
array = [
|
2772
|
+
[:key, :value],
|
2773
|
+
kvp,
|
2774
|
+
]
|
2775
|
+
== {key: :value, obtained: :via_to_ary}, array.to_h
|
2776
|
+
e = assert_raise(TypeError){
|
2777
|
+
[[:first_one, :ok], :not_ok].to_h
|
2778
|
+
}
|
2779
|
+
assert_equal "wrong element type Symbol at 1 (expected array)", e.message
|
2780
|
+
e = assert_raise(ArgumentError){
|
2781
|
+
[[:first_one, :ok], [1, 2], [:not_ok]].to_h
|
2782
|
+
}
|
2783
|
+
assert_equal "wrong array length at 2 (expected 2, was 1)", e.message
|
2784
|
+
|
2785
|
+
"? ruby/test/ruby/test_array.rb:test_uniq",
|
2786
|
+
a = []
|
2787
|
+
b = a.uniq
|
2788
|
+
== [], a
|
2789
|
+
== [], b
|
2790
|
+
assert_not_same(a, b)
|
2791
|
+
a = [1]
|
2792
|
+
b = a.uniq
|
2793
|
+
== [1], a
|
2794
|
+
== [1], b
|
2795
|
+
assert_not_same(a, b)
|
2796
|
+
a = [1,1]
|
2797
|
+
b = a.uniq
|
2798
|
+
== [1,1], a
|
2799
|
+
== [1], b
|
2800
|
+
assert_not_same(a, b)
|
2801
|
+
a = [1,2]
|
2802
|
+
b = a.uniq
|
2803
|
+
== [1,2], a
|
2804
|
+
== [1,2], b
|
2805
|
+
assert_not_same(a, b)
|
2806
|
+
a = Array[1, 2, 3, 2, 1, 2, 3, 4, nil]
|
2807
|
+
b = a.dup
|
2808
|
+
== Array[1, 2, 3, 4, nil], a.uniq
|
2809
|
+
== b, a
|
2810
|
+
c = Array["a:def", "a:xyz", "b:abc", "b:xyz", "c:jkl"]
|
2811
|
+
d = c.dup
|
2812
|
+
== Array["a:def", "b:abc", "c:jkl"], c.uniq{|s| s[/^\w+/]}
|
2813
|
+
== d, c
|
2814
|
+
== Array[1, 2, 3], Array[1, 2, 3].uniq
|
2815
|
+
a = %w(a a)
|
2816
|
+
b = a.uniq
|
2817
|
+
== %w(a a), a
|
2818
|
+
assert(a.none?(&:frozen?)
|
2819
|
+
== %w(a), b
|
2820
|
+
assert(b.none?(&:frozen?)
|
2821
|
+
bug9340 = "ruby-core:59457"
|
2822
|
+
ary = [bug9340, bug9340.dup, bug9340.dup]
|
2823
|
+
assert_equal 1, ary.uniq.size
|
2824
|
+
assert_same bug9340, ary.uniq[0]
|
2825
|
+
|
2826
|
+
"? ruby/test/ruby/test_array.rb:test_uniq_with_block",
|
2827
|
+
a = []
|
2828
|
+
b = a.uniq{|v| v.even?}
|
2829
|
+
== [], a
|
2830
|
+
== [], b
|
2831
|
+
assert_not_same(a, b)
|
2832
|
+
a = [1]
|
2833
|
+
b = a.uniq{|v| v.even?}
|
2834
|
+
== [1], a
|
2835
|
+
== [1], b
|
2836
|
+
assert_not_same(a, b)
|
2837
|
+
a = [1,3]
|
2838
|
+
b = a.uniq{|v| v.even?}
|
2839
|
+
== [1,3], a
|
2840
|
+
== [1], b
|
2841
|
+
assert_not_same(a, b)
|
2842
|
+
a = %w(a a)
|
2843
|
+
b = a.uniq{|v| v}
|
2844
|
+
== %w(a a), a
|
2845
|
+
assert(a.none?(&:frozen?)
|
2846
|
+
== %w(a), b
|
2847
|
+
assert(b.none?(&:frozen?)
|
2848
|
+
|
2849
|
+
"? ruby/test/ruby/test_array.rb:test_uniq!",
|
2850
|
+
a = []
|
2851
|
+
b = a.uniq!
|
2852
|
+
b.nil?
|
2853
|
+
a = [1]
|
2854
|
+
b = a.uniq!
|
2855
|
+
b.nil?
|
2856
|
+
a = [1,1]
|
2857
|
+
b = a.uniq!
|
2858
|
+
== [1], a
|
2859
|
+
== [1], b
|
2860
|
+
assert_same(a, b)
|
2861
|
+
a = [1,2]
|
2862
|
+
b = a.uniq!
|
2863
|
+
== [1,2], a
|
2864
|
+
b.nil?
|
2865
|
+
a = Array[1, 2, 3, 2, 1, 2, 3, 4, nil]
|
2866
|
+
== Array[1, 2, 3, 4, nil], a.uniq!
|
2867
|
+
== Array[1, 2, 3, 4, nil], a
|
2868
|
+
c = Array["a:def", "a:xyz", "b:abc", "b:xyz", "c:jkl"]
|
2869
|
+
== Array["a:def", "b:abc", "c:jkl"], c.uniq!{|s| s[/^\w+/]}
|
2870
|
+
== Array["a:def", "b:abc", "c:jkl"], c
|
2871
|
+
c = Array["a:def", "b:abc", "c:jkl"]
|
2872
|
+
c.uniq!{|s| s[/^\w+/]}.nil?
|
2873
|
+
== Array["a:def", "b:abc", "c:jkl"], c
|
2874
|
+
assert_nil(Array[1, 2, 3].uniq!)
|
2875
|
+
f = a.dup
|
2876
|
+
a.uniq!(1).raise?(ArgumentError)
|
2877
|
+
f.uniq!(1).raise?(ArgumentError)
|
2878
|
+
f.uniq!.raise?(RuntimeError)
|
2879
|
+
assert_nothing_raised do
|
2880
|
+
a = [{c: "b"}, {c: "r"}, {c: "w"}, {c: "g"}, {c: "g"}]
|
2881
|
+
a.sort_by!{|e| e[:c]}
|
2882
|
+
a.uniq!{|e| e[:c]}
|
2883
|
+
end
|
2884
|
+
a = %w(a a)
|
2885
|
+
b = a.uniq
|
2886
|
+
== %w(a a), a
|
2887
|
+
assert(a.none?(&:frozen?)
|
2888
|
+
== %w(a), b
|
2889
|
+
assert(b.none?(&:frozen?)
|
2890
|
+
|
2891
|
+
"? ruby/test/ruby/test_array.rb:test_uniq_bang_with_block",
|
2892
|
+
a = []
|
2893
|
+
b = a.uniq!{|v| v.even?}
|
2894
|
+
b.nil?
|
2895
|
+
a = [1]
|
2896
|
+
b = a.uniq!{|v| v.even?}
|
2897
|
+
b.nil?
|
2898
|
+
a = [1,3]
|
2899
|
+
b = a.uniq!{|v| v.even?}
|
2900
|
+
== [1], a
|
2901
|
+
== [1], b
|
2902
|
+
assert_same(a, b)
|
2903
|
+
a = [1,2]
|
2904
|
+
b = a.uniq!{|v| v.even?}
|
2905
|
+
== [1,2], a
|
2906
|
+
b.nil?
|
2907
|
+
a = %w(a a)
|
2908
|
+
b = a.uniq!{|v| v}
|
2909
|
+
== %w(a), b
|
2910
|
+
assert_same(a, b)
|
2911
|
+
assert b.none?(&:frozen?)
|
2912
|
+
|
2913
|
+
"? ruby/test/ruby/test_array.rb:test_uniq_bang_with_freeze",
|
2914
|
+
ary = [1,2]
|
2915
|
+
orig = ary.dup
|
2916
|
+
ary.uniq!{|v| ary; 1}.raise?(RuntimeError, message: "frozen during comparison")
|
2917
|
+
== orig, ary, "must not be modified once frozen"
|
2918
|
+
|
2919
|
+
"? ruby/test/ruby/test_array.rb:test_unshift",
|
2920
|
+
a = Array[]
|
2921
|
+
== Array["cat"], a.unshift("cat")
|
2922
|
+
== Array["dog", "cat"], a.unshift("dog")
|
2923
|
+
== Array[nil, "dog", "cat"], a.unshift(nil)
|
2924
|
+
== Array[Array[1,2], nil, "dog", "cat"], a.unshift(Array[1, 2])
|
2925
|
+
|
2926
|
+
"? ruby/test/ruby/test_array.rb:test_OR # '|'",
|
2927
|
+
== Array[], Array[] | Array[]
|
2928
|
+
== Array[1], Array[1] | Array[]
|
2929
|
+
== Array[1], Array[] | Array[1]
|
2930
|
+
== Array[1], Array[1] | Array[1]
|
2931
|
+
== Array[1,2], Array[1] | Array[2]
|
2932
|
+
== Array[1,2], Array[1, 1] | Array[2, 2]
|
2933
|
+
== Array[1,2], Array[1, 2] | Array[1, 2]
|
2934
|
+
a = %w(a b c)
|
2935
|
+
b = %w(a b c d e)
|
2936
|
+
c = a | b
|
2937
|
+
== c, b
|
2938
|
+
assert_not_same(c, b)
|
2939
|
+
== %w(a b c), a
|
2940
|
+
== %w(a b c d e), b
|
2941
|
+
assert(a.none?(&:frozen?)
|
2942
|
+
assert(b.none?(&:frozen?)
|
2943
|
+
assert(c.none?(&:frozen?)
|
2944
|
+
|
2945
|
+
"? ruby/test/ruby/test_array.rb:test_OR_in_order",
|
2946
|
+
obj1, obj2 = Class.new do
|
2947
|
+
attr_reader :name
|
2948
|
+
def initialize(name) @name = name; end
|
2949
|
+
def inspect; "test_OR_in_order(#{@name})"; end
|
2950
|
+
def hash; 0; end
|
2951
|
+
def eql?(a) true; end
|
2952
|
+
break [new("1"), new("2")]
|
2953
|
+
end
|
2954
|
+
== [obj1], [obj1]|[obj2]
|
2955
|
+
|
2956
|
+
"? ruby/test/ruby/test_array.rb:test_repeated_permutation",
|
2957
|
+
a = Array[1,2]
|
2958
|
+
== Array[[]], a.repeated_permutation(0).to_a
|
2959
|
+
== Array[[1],[2]], a.repeated_permutation(1).to_a.sort
|
2960
|
+
== Array[[1,1],[1,2],[2,1],[2,2]],
|
2961
|
+
a.repeated_permutation(2).to_a.sort
|
2962
|
+
== Array[[1,1,1],[1,1,2],[1,2,1],[1,2,2],
|
2963
|
+
[2,1,1],[2,1,2],[2,2,1],[2,2,2]],
|
2964
|
+
a.repeated_permutation(3).to_a.sort
|
2965
|
+
== Array[], a.repeated_permutation(-1).to_a
|
2966
|
+
== "abcde".each_char.to_a.repeated_permutation(5).sort,
|
2967
|
+
"edcba".each_char.to_a.repeated_permutation(5).sort
|
2968
|
+
== Array[].repeated_permutation(0).to_a, Array[[]]
|
2969
|
+
== Array[].repeated_permutation(1).to_a, Array[]
|
2970
|
+
a = Array[1, 2, 3, 4]
|
2971
|
+
b = Array[]
|
2972
|
+
a.repeated_permutation(4){|x| b << x; a.replace(Array[9, 8, 7, 6])}
|
2973
|
+
== Array[9, 8, 7, 6], a
|
2974
|
+
== Array[1, 2, 3, 4].repeated_permutation(4).to_a, b
|
2975
|
+
a = Array[0, 1, 2, 3, 4][1, 4].repeated_permutation(2)
|
2976
|
+
assert_empty(a.reject{|x| !x.include?(0)})
|
2977
|
+
assert_separately([], <<~"end;") # do
|
2978
|
+
assert_nothing_raised(SystemStackError) do
|
2979
|
+
== :ok, Array.new(100_000, nil).repeated_permutation(500_000){break :ok}
|
2980
|
+
end
|
2981
|
+
end;
|
2982
|
+
|
2983
|
+
"? ruby/test/ruby/test_array.rb:test_repeated_combination",
|
2984
|
+
a = Array[1,2,3]
|
2985
|
+
== Array[[]], a.repeated_combination(0).to_a
|
2986
|
+
== Array[[1],[2],[3]], a.repeated_combination(1).to_a.sort
|
2987
|
+
== Array[[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]],
|
2988
|
+
a.repeated_combination(2).to_a.sort
|
2989
|
+
== Array[[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],
|
2990
|
+
[1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]],
|
2991
|
+
a.repeated_combination(3).to_a.sort
|
2992
|
+
== Array[[1,1,1,1],[1,1,1,2],[1,1,1,3],[1,1,2,2],[1,1,2,3],
|
2993
|
+
[1,1,3,3],[1,2,2,2],[1,2,2,3],[1,2,3,3],[1,3,3,3],
|
2994
|
+
[2,2,2,2],[2,2,2,3],[2,2,3,3],[2,3,3,3],[3,3,3,3]],
|
2995
|
+
a.repeated_combination(4).to_a.sort
|
2996
|
+
== Array[], a.repeated_combination(-1).to_a
|
2997
|
+
== "abcde".each_char.to_a.repeated_combination(5).map{|e|e.sort}.sort,
|
2998
|
+
"edcba".each_char.to_a.repeated_combination(5).map{|e|e.sort}.sort
|
2999
|
+
== Array[].repeated_combination(0).to_a, Array[[]]
|
3000
|
+
== Array[].repeated_combination(1).to_a, Array[]
|
3001
|
+
a = Array[1, 2, 3, 4]
|
3002
|
+
b = Array[]
|
3003
|
+
a.repeated_combination(4){|x| b << x; a.replace(Array[9, 8, 7, 6])}
|
3004
|
+
== Array[9, 8, 7, 6], a
|
3005
|
+
== Array[1, 2, 3, 4].repeated_combination(4).to_a, b
|
3006
|
+
a = Array[0, 1, 2, 3, 4][1, 4].repeated_combination(2)
|
3007
|
+
assert_empty(a.reject{|x| !x.include?(0)})
|
3008
|
+
assert_separately([], <<~"end;") # do
|
3009
|
+
assert_nothing_raised(SystemStackError) do
|
3010
|
+
== :ok, Array.new(100_000, nil).repeated_combination(500_000){break :ok}
|
3011
|
+
end
|
3012
|
+
end;
|
3013
|
+
|
3014
|
+
"? ruby/test/ruby/test_array.rb:test_drop_while",
|
3015
|
+
== [3,4,5,0], [1,2,3,4,5,0].drop_while{|i| i < 3}
|
3016
|
+
|
3017
|
+
"? ruby/test/ruby/test_array.rb:test_ary_new",
|
3018
|
+
[].to_enum.first(-1).raise?(ArgumentError)
|
3019
|
+
[].to_enum.first(longp).raise?(ArgumentError)
|
3020
|
+
|
3021
|
+
"? ruby/test/ruby/test_array.rb:test_initialize",
|
3022
|
+
assert_nothing_raised{[].instance_eval{initialize}}
|
3023
|
+
assert_nothing_raised{Array.new{}}
|
3024
|
+
== [1, 2, 3], Array.new([1, 2, 3]
|
3025
|
+
Array.new(-1, 1).raise?(ArgumentError)
|
3026
|
+
Array.new(longp, 1).raise?(ArgumentError)
|
3027
|
+
== [1, 1, 1], Array.new(3, 1)
|
3028
|
+
== [1, 1, 1], Array.new(3){1}
|
3029
|
+
== [1, 1, 1], Array.new(3, 1){1}
|
3030
|
+
|
3031
|
+
"? ruby/test/ruby/test_array.rb:test_aset_error",
|
3032
|
+
[0][-2] = 1.raise?(IndexError)
|
3033
|
+
[0][longp] = 2.raise?(IndexError)
|
3034
|
+
[0][(longp + 1) / 2 - 1] = 2.raise?(IndexError)
|
3035
|
+
[0][longp..-1] = 2.raise?(IndexError)
|
3036
|
+
a = [0]
|
3037
|
+
a[2] = 4
|
3038
|
+
== [0, nil, 4], a
|
3039
|
+
([0][0, 0, 0] = 0).raise?(ArgumentError)
|
3040
|
+
([0][0, 0, 0] = 0).raise?(ArgumentError)
|
3041
|
+
([0][:foo] = 0).raise?(TypeError)
|
3042
|
+
([0][:foo] = 0).raise?(RuntimeError)
|
3043
|
+
|
3044
|
+
"? ruby/test/ruby/test_array.rb:test_last2",
|
3045
|
+
== [0], [0].last(2)
|
3046
|
+
[0].last(-1).raise?(ArgumentError)
|
3047
|
+
|
3048
|
+
"? ruby/test/ruby/test_array.rb:test_shift2",
|
3049
|
+
== 0, ([0] * 16).shift
|
3050
|
+
# check
|
3051
|
+
a = [0, 1, 2]
|
3052
|
+
a[3] = 3
|
3053
|
+
a.shift(2)
|
3054
|
+
== [2, 3], a
|
3055
|
+
|
3056
|
+
"? ruby/test/ruby/test_array.rb:test_index2",
|
3057
|
+
a = [0, 1, 2]
|
3058
|
+
== a, a.index.to_a
|
3059
|
+
== 1, a.index{|x| x == 1}
|
3060
|
+
|
3061
|
+
"? ruby/test/ruby/test_array.rb:test_rindex2",
|
3062
|
+
a = [0, 1, 2]
|
3063
|
+
== [2, 1, 0], a.rindex.to_a
|
3064
|
+
== 1, a.rindex{|x| x == 1}
|
3065
|
+
a = [0, 1]
|
3066
|
+
e = a.rindex
|
3067
|
+
== 1, e.next
|
3068
|
+
a.clear
|
3069
|
+
e.next.raise?(StopIteration)
|
3070
|
+
o = Object.new
|
3071
|
+
class << o; self; end.class_eval do
|
3072
|
+
define_method(:==){|x| a.clear; false}
|
3073
|
+
end
|
3074
|
+
a = [nil, o]
|
3075
|
+
a.rindex(0).nil?
|
3076
|
+
|
3077
|
+
"? ruby/test/ruby/test_array.rb:test_ary_to_ary",
|
3078
|
+
o = Object.new
|
3079
|
+
def o.to_ary; [1, 2, 3]; end
|
3080
|
+
a, b, c = o
|
3081
|
+
== [1, 2, 3], [a, b, c]
|
3082
|
+
|
3083
|
+
"? ruby/test/ruby/test_array.rb:test_insert",
|
3084
|
+
a = [0]
|
3085
|
+
== [0], a.insert(1)
|
3086
|
+
== [0, 1], a.insert(1, 1)
|
3087
|
+
a.insert.raise?(ArgumentError)
|
3088
|
+
== [0, 1, 2], a.insert(-1, 2)
|
3089
|
+
== [0, 1, 3, 2], a.insert(-2, 3)
|
3090
|
+
[0].insert(0).raise?(RuntimeError)
|
3091
|
+
[0].insert.raise?(ArgumentError)
|
3092
|
+
|
3093
|
+
"? ruby/test/ruby/test_array.rb:test_join2",
|
3094
|
+
a = []
|
3095
|
+
a << a
|
3096
|
+
a.join.raise?(ArgumentError)
|
3097
|
+
def (a = Object.new).to_ary
|
3098
|
+
[self]
|
3099
|
+
end
|
3100
|
+
[a].join.raise?(ArgumentError, message: "ruby-core:24150")
|
3101
|
+
== "12345", [1,[2,[3,4],5]].join
|
3102
|
+
|
3103
|
+
"? ruby/test/ruby/test_array.rb:test_to_a2",
|
3104
|
+
klass = Class.new(Array)
|
3105
|
+
a = klass.new.to_a
|
3106
|
+
== [], a
|
3107
|
+
== Array, a.class
|
3108
|
+
|
3109
|
+
"? ruby/test/ruby/test_array.rb:test_values_at2",
|
3110
|
+
a = [0, 1, 2, 3, 4, 5]
|
3111
|
+
== [1, 2, 3], a.values_at(1..3)
|
3112
|
+
== [nil, nil], a.values_at(7..8)
|
3113
|
+
bug6203 = "ruby-core:43678"
|
3114
|
+
== [4, 5, nil, nil], a.values_at(4..7), bug6203
|
3115
|
+
== [nil], a.values_at(2**31-1)
|
3116
|
+
|
3117
|
+
"? ruby/test/ruby/test_array.rb:test_select",
|
3118
|
+
== [0, 2], [0, 1, 2, 3].select{|x| (x % 2).zero?}
|
3119
|
+
|
3120
|
+
# also keep_if
|
3121
|
+
"? ruby/test/ruby/test_array.rb:test_select!",
|
3122
|
+
a = Array[1, 2, 3, 4, 5]
|
3123
|
+
a.select!{true}.nil?
|
3124
|
+
== a, a.keep_if{true}
|
3125
|
+
== Array[1, 2, 3, 4, 5], a
|
3126
|
+
a = Array[1, 2, 3, 4, 5]
|
3127
|
+
== a, a.select!{false}
|
3128
|
+
== Array[], a
|
3129
|
+
a = Array[1, 2, 3, 4, 5]
|
3130
|
+
== a, a.select!{|i| i > 3}
|
3131
|
+
== Array[4, 5], a
|
3132
|
+
|
3133
|
+
"? ruby/test/ruby/test_array.rb:test_delete2",
|
3134
|
+
a = [0] * 1024 + [1] + [0] * 1024
|
3135
|
+
a.delete(0)
|
3136
|
+
== [1], a
|
3137
|
+
|
3138
|
+
"? ruby/test/ruby/test_array.rb:test_reject_with_callcc",
|
3139
|
+
respond_to?(:callcc, true) or require "continuation"
|
3140
|
+
bug9727 = "ruby-dev:48101 Bug #9727"
|
3141
|
+
cont = nil
|
3142
|
+
a = [*1..10].reject do
|
3143
|
+
|i|
|
3144
|
+
callcc{|c| cont = c} if !cont and i == 10
|
3145
|
+
false
|
3146
|
+
end
|
3147
|
+
if a.size < 1000
|
3148
|
+
a.unshift(:x)
|
3149
|
+
cont.call
|
3150
|
+
end
|
3151
|
+
== 1000, a.size, bug9727
|
3152
|
+
== [:x, *1..10], a.uniq, bug9727
|
3153
|
+
|
3154
|
+
"? ruby/test/ruby/test_array.rb:test_zip",
|
3155
|
+
== [[1, :a, "a"], [2, :b, "b"], [3, nil, "c"]],
|
3156
|
+
[1, 2, 3].zip([:a, :b], ["a", "b", "c", "d"])
|
3157
|
+
a = []
|
3158
|
+
[1, 2, 3].zip([:a, :b], ["a", "b", "c", "d"]){|x| a << x}
|
3159
|
+
== [[1, :a, "a"], [2, :b, "b"], [3, nil, "c"]], a
|
3160
|
+
ary = Object.new
|
3161
|
+
def ary.to_a; [1, 2]; end
|
3162
|
+
%w(a b).zip(ary).raise?(TypeError)
|
3163
|
+
def ary.each; [3, 4].each{|e|yield e}; end
|
3164
|
+
== [["a", 3], ["b", 4]], %w(a b).zip(ary)
|
3165
|
+
def ary.to_ary; [5, 6]; end
|
3166
|
+
== [["a", 5], ["b", 6]], %w(a b).zip(ary)
|
3167
|
+
|
3168
|
+
"? ruby/test/ruby/test_array.rb:test_zip_bug",
|
3169
|
+
bug8153 = "ruby-core:53650"
|
3170
|
+
r = 1..1
|
3171
|
+
def r.respond_to?(*)
|
3172
|
+
super
|
3173
|
+
end
|
3174
|
+
assert_equal [[42, 1]], [42].zip(r), bug8153
|
3175
|
+
|
3176
|
+
"? ruby/test/ruby/test_array.rb:test_equal",
|
3177
|
+
o = Object.new
|
3178
|
+
def o.to_ary; end
|
3179
|
+
def o.==(x); :foo; end
|
3180
|
+
== [0, 1, 2], o
|
3181
|
+
assert_not_equal([0, 1, 2], [0, 1, 3])
|
3182
|
+
A = Array.new(3, &:to_s)
|
3183
|
+
B = A.dup
|
3184
|
+
|
3185
|
+
"? ruby/test/ruby/test_array.rb:test_equal_resize",
|
3186
|
+
o = Object.new
|
3187
|
+
def o.==(o)
|
3188
|
+
A.clear
|
3189
|
+
B.clear
|
3190
|
+
true
|
3191
|
+
end
|
3192
|
+
A[1] = o
|
3193
|
+
== A, B
|
3194
|
+
|
3195
|
+
"? ruby/test/ruby/test_array.rb:test_flatten_error",
|
3196
|
+
a = []
|
3197
|
+
a << a
|
3198
|
+
a.flatten.raise?(ArgumentError)
|
3199
|
+
f = []
|
3200
|
+
a.flatten!(1, 2).raise?(ArgumentError)
|
3201
|
+
a.flatten!(:foo).raise?(TypeError)
|
3202
|
+
f.flatten!(1, 2).raise?(ArgumentError)
|
3203
|
+
f.flatten!.raise?(RuntimeError)
|
3204
|
+
f.flatten!(:foo).raise?(RuntimeError)
|
3205
|
+
|
3206
|
+
"? ruby/test/ruby/test_array.rb:test_shuffle",
|
3207
|
+
100.times do
|
3208
|
+
== [0, 1, 2], [2, 1, 0].shuffle.sort
|
3209
|
+
end
|
3210
|
+
gen = Random.new(0)
|
3211
|
+
[1, 2, 3].shuffle(1, random: gen).raise?(ArgumentError)
|
3212
|
+
srand(0)
|
3213
|
+
100.times do
|
3214
|
+
== [0, 1, 2].shuffle, [0, 1, 2].shuffle(random: gen)
|
3215
|
+
end
|
3216
|
+
[0, 1, 2].shuffle(xawqij: "a").raise?(ArgumentError, message: /unknown keyword/)
|
3217
|
+
[0, 1, 2].shuffle!(xawqij: "a").raise?(ArgumentError, message: /unknown keyword/)
|
3218
|
+
|
3219
|
+
"? ruby/test/ruby/test_array.rb:test_shuffle_random",
|
3220
|
+
gen = proc do
|
3221
|
+
10000000
|
3222
|
+
end
|
3223
|
+
class << gen
|
3224
|
+
alias rand call # Use `prepend`
|
3225
|
+
end
|
3226
|
+
[*0..2].shuffle(random: gen).raise?(RangeError)
|
3227
|
+
ary = (0...10000).to_a
|
3228
|
+
gen = proc do
|
3229
|
+
ary.replace([])
|
3230
|
+
0.5
|
3231
|
+
end
|
3232
|
+
class << gen
|
3233
|
+
alias rand call # Use `prepend`
|
3234
|
+
end
|
3235
|
+
ary.shuffle!(random: gen).raise?(RuntimeError)
|
3236
|
+
zero = Object.new
|
3237
|
+
def zero.to_int
|
3238
|
+
0
|
3239
|
+
end
|
3240
|
+
gen_to_int = proc do
|
3241
|
+
|max|
|
3242
|
+
zero
|
3243
|
+
end
|
3244
|
+
class << gen_to_int
|
3245
|
+
alias rand call # Use `prepend`
|
3246
|
+
end
|
3247
|
+
ary = (0...10000).to_a
|
3248
|
+
== ary.rotate, ary.shuffle(random: gen_to_int)
|
3249
|
+
|
3250
|
+
"? ruby/test/ruby/test_array.rb:test_sample",
|
3251
|
+
100.times do
|
3252
|
+
assert_include([0, 1, 2], [2, 1, 0].sample)
|
3253
|
+
samples = [2, 1, 0].sample(2)
|
3254
|
+
samples.each{|sample|
|
3255
|
+
assert_include([0, 1, 2], sample)
|
3256
|
+
}
|
3257
|
+
end
|
3258
|
+
srand(0)
|
3259
|
+
a = (1..18).to_a
|
3260
|
+
(0..20).each do
|
3261
|
+
|n|
|
3262
|
+
100.times do
|
3263
|
+
b = a.sample(n)
|
3264
|
+
== [n, 18].min, b.size
|
3265
|
+
== a, (a | b).sort
|
3266
|
+
== b.sort, (a & b).sort
|
3267
|
+
end
|
3268
|
+
h = Hash.new(0)
|
3269
|
+
1000.times do
|
3270
|
+
a.sample(n).each{|x| h[x] += 1}
|
3271
|
+
end
|
3272
|
+
assert_operator(h.values.min * 2, :>=, h.values.max) if n != 0
|
3273
|
+
end
|
3274
|
+
[1, 2].sample(-1).raise?(ArgumentError, message: "ruby-core:23374")
|
3275
|
+
gen = Random.new(0)
|
3276
|
+
srand(0)
|
3277
|
+
a = (1..18).to_a
|
3278
|
+
(0..20).each do
|
3279
|
+
|n|
|
3280
|
+
100.times do
|
3281
|
+
|i|
|
3282
|
+
== a.sample(n), a.sample(n, random: gen), "#{i}/#{n}"
|
3283
|
+
end
|
3284
|
+
end
|
3285
|
+
[0, 1, 2].sample(xawqij: "a").raise?(ArgumentError, message: /unknown keyword/)
|
3286
|
+
|
3287
|
+
"? ruby/test/ruby/test_array.rb:test_sample_random",
|
3288
|
+
ary = (0...10000).to_a
|
3289
|
+
ary.sample(1, 2, random: nil).raise?(ArgumentError)
|
3290
|
+
gen0 = proc do
|
3291
|
+
|max|
|
3292
|
+
max/2
|
3293
|
+
end
|
3294
|
+
class << gen0
|
3295
|
+
alias rand call # Use `prepend`
|
3296
|
+
end
|
3297
|
+
gen1 = proc do
|
3298
|
+
|max|
|
3299
|
+
ary.replace([])
|
3300
|
+
max/2
|
3301
|
+
end
|
3302
|
+
class << gen1
|
3303
|
+
alias rand call # Use `prepend`
|
3304
|
+
end
|
3305
|
+
== 5000, ary.sample(random: gen0)
|
3306
|
+
assert_nil(ary.sample(random: gen1)
|
3307
|
+
== [], ary
|
3308
|
+
ary = (0...10000).to_a
|
3309
|
+
== [5000], ary.sample(1, random: gen0)
|
3310
|
+
== [], ary.sample(1, random: gen1)
|
3311
|
+
== [], ary
|
3312
|
+
ary = (0...10000).to_a
|
3313
|
+
== [5000, 4999], ary.sample(2, random: gen0)
|
3314
|
+
== [], ary.sample(2, random: gen1)
|
3315
|
+
== [], ary
|
3316
|
+
ary = (0...10000).to_a
|
3317
|
+
== [5000, 4999, 5001], ary.sample(3, random: gen0)
|
3318
|
+
== [], ary.sample(3, random: gen1)
|
3319
|
+
== [], ary
|
3320
|
+
ary = (0...10000).to_a
|
3321
|
+
== [5000, 4999, 5001, 4998], ary.sample(4, random: gen0)
|
3322
|
+
== [], ary.sample(4, random: gen1)
|
3323
|
+
== [], ary
|
3324
|
+
ary = (0...10000).to_a
|
3325
|
+
== [5000, 4999, 5001, 4998, 5002, 4997, 5003, 4996, 5004, 4995], ary.sample(10, random: gen0)
|
3326
|
+
== [], ary.sample(10, random: gen1)
|
3327
|
+
== [], ary
|
3328
|
+
ary = (0...10000).to_a
|
3329
|
+
== [5000, 0, 5001, 2, 5002, 4, 5003, 6, 5004, 8, 5005], ary.sample(11, random: gen0)
|
3330
|
+
ary.sample(11, random: gen1) # implementation decoda, may change in the future
|
3331
|
+
== [], ary
|
3332
|
+
half = Object.new
|
3333
|
+
def half.to_int
|
3334
|
+
5000
|
3335
|
+
end
|
3336
|
+
gen_to_int = proc do
|
3337
|
+
|max|
|
3338
|
+
half
|
3339
|
+
end
|
3340
|
+
class << gen_to_int
|
3341
|
+
alias rand call # Use `prepend`
|
3342
|
+
end
|
3343
|
+
ary = (0...10000).to_a
|
3344
|
+
== 5000, ary.sample(random: gen_to_int)
|
3345
|
+
|
3346
|
+
"? ruby/test/ruby/test_array.rb:test_cycle",
|
3347
|
+
a = []
|
3348
|
+
[0, 1, 2].cycle do
|
3349
|
+
|i|
|
3350
|
+
a << i
|
3351
|
+
break if a.size == 10
|
3352
|
+
end
|
3353
|
+
== [0, 1, 2, 0, 1, 2, 0, 1, 2, 0], a
|
3354
|
+
a = [0, 1, 2]
|
3355
|
+
assert_nil(a.cycle{a.clear})
|
3356
|
+
a = []
|
3357
|
+
[0, 1, 2].cycle(3){|i| a << i}
|
3358
|
+
== [0, 1, 2, 0, 1, 2, 0, 1, 2], a
|
3359
|
+
|
3360
|
+
"? ruby/test/ruby/test_array.rb:test_reverse_each2",
|
3361
|
+
a = [0, 1, 2, 3, 4, 5]
|
3362
|
+
r = []
|
3363
|
+
a.reverse_each do
|
3364
|
+
|x|
|
3365
|
+
r << x
|
3366
|
+
a.pop
|
3367
|
+
a.pop
|
3368
|
+
end
|
3369
|
+
== [5, 3, 1], r
|
3370
|
+
|
3371
|
+
"? ruby/test/ruby/test_array.rb:test_combination_clear",
|
3372
|
+
bug9939 = "ruby-core:63149 Bug #9939"
|
3373
|
+
assert_separately([], <<~"end;")
|
3374
|
+
100_000.times{Array.new(1000)}
|
3375
|
+
a = [*0..100]
|
3376
|
+
a.combination(3){|*,x| a.clear}
|
3377
|
+
end;
|
3378
|
+
|
3379
|
+
"? ruby/test/ruby/test_array.rb:test_product2",
|
3380
|
+
a = (0..100).to_a
|
3381
|
+
a.product(a, a, a, a, a, a, a, a, a, a).raise?(RangeError)
|
3382
|
+
assert_nothing_raised(RangeError) do
|
3383
|
+
a.product(a, a, a, a, a, a, a, a, a, a){break}
|
3384
|
+
end
|
3385
|
+
|
3386
|
+
"? ruby/test/ruby/test_array.rb:ruby/test/ruby/test_array_subclass",
|
3387
|
+
class Array2 < Array; end
|
3388
|
+
== Array2, Array2[1,2,3].uniq.class, "ruby-dev:34581"
|
3389
|
+
== Array2, Array2[1,2][0,1].class # embeded
|
3390
|
+
== Array2, Array2[*(1..100)][1..99].class #not embeded
|
3391
|
+
|
3392
|
+
"? ruby/test/ruby/test_array.rb:test_inspect",
|
3393
|
+
a = Array[1, 2, 3]
|
3394
|
+
a.taint
|
3395
|
+
s = a.inspect
|
3396
|
+
== true, s.tainted?
|
3397
|
+
|
3398
|
+
"? ruby/test/ruby/test_array.rb:test_initialize2",
|
3399
|
+
a = [1] * 1000
|
3400
|
+
a.instance_eval{initialize}
|
3401
|
+
== [], a
|
3402
|
+
|
3403
|
+
"? ruby/test/ruby/test_array.rb:test_shift_shared_ary",
|
3404
|
+
a = (1..100).to_a
|
3405
|
+
b = []
|
3406
|
+
b.replace(a)
|
3407
|
+
== (1..10).to_a, a.shift(10)
|
3408
|
+
== (11..100).to_a, a
|
3409
|
+
|
3410
|
+
"? ruby/test/ruby/test_array.rb:test_fill_negative_length",
|
3411
|
+
a = (1..10).to_a
|
3412
|
+
a.fill(:foo, 5, -3)
|
3413
|
+
== (1..10).to_a, a
|
3414
|
+
|
3415
|
+
"? ruby/test/ruby/test_array.rb:test_shared_marking",
|
3416
|
+
reduce = proc do
|
3417
|
+
|s|
|
3418
|
+
s.gsub(/(verify_internal_consistency_reachable_i:\sWB\smiss\s\S+\s\(T_ARRAY\)\s->\s)\S+\s\((proc|T_NONE)\)\n
|
3419
|
+
\K(?:\1\S+\s\(\2\)\n)*/x) do
|
3420
|
+
"...(snip #{$&.count("\n")} lines)...\n"
|
3421
|
+
end
|
3422
|
+
end
|
3423
|
+
begin
|
3424
|
+
assert_normal_exit(<<~EOS, "[Bug #9718]", timeout: 5, stdout_filter: reduce)
|
3425
|
+
queue = []
|
3426
|
+
50.times do
|
3427
|
+
10_000.times do
|
3428
|
+
queue << lambda{}
|
3429
|
+
end
|
3430
|
+
GC.start(full_mark: false, immediate_sweep: true)
|
3431
|
+
GC.verify_internal_consistency
|
3432
|
+
queue.shift.call
|
3433
|
+
end
|
3434
|
+
EOS
|
3435
|
+
rescue Timeout::Error => e
|
3436
|
+
skip e.message
|
3437
|
+
end
|
3438
|
+
=end
|