Arrays are ordered, integer-indexed collections of any object.
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.1. Creating Arrays
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
:ary = [1, "two", 3.0] #=> [1, "two", 3.0]
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).ary = Array.new #=> [] Array.new(3) #=> [nil, nil, nil] Array.new(3, true) #=> [true, true, true]
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
.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:
Array.new(4){Hash.new} #=> [{}, {}, {}, {}]
This is also a quick way to build up multi-dimensional arrays:
empty_table = Array.new(3){Array.new(3)} #=> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
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.Array({:a => "a", :b => "b"}) #=> [[:a, "a"], [:b, "b"]]
2. Example Usage
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.Some of the more common ones are illustrated below.
3. Accessing Elements
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.arr = [1, 2, 3, 4, 5, 6] arr[2] #=> 3 arr[100] #=> nil arr[-3] #=> 4 arr[2, 3] #=> [3, 4, 5] arr[1..4] #=> [2, 3, 4, 5] arr[1..-3] #=> [2, 3, 4]
Another way to access a particular array element is by using the at method
arr.at(0) #=> 1
The slice method works in an identical manner to
#[]
.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
.arr = ["a", "b", "c", "d", "e", "f"] arr.fetch(100) #=> IndexError: index 100 outside of array bounds: -6...6 arr.fetch(100, "oops") #=> "oops"
The special methods
first
and last
will return the first and last elements of an array, respectively.arr.first #=> 1 arr.last #=> 6
To return the first n elements of an array, use take
arr.take(3) #=> [1, 2, 3]
drop
does the opposite of take
, by returning the elements after n elements have been dropped:arr.drop(3) #=> [4, 5, 6]
4. Obtaining Information about an Array
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
.browsers = ["Chrome", "Firefox", "Safari", "Opera", "IE"] browsers.length #=> 5 browsers.count #=> 5
To check whether an array contains any elements at all
browsers.empty? #=> false
To check whether a particular item is included in the array
browsers.include?("Konqueror") #=> false
5. Adding Items to Arrays
Items can be added to the end of an array by using either
push
or <<
arr = [1, 2, 3, 4] arr.push(5) #=> [1, 2, 3, 4, 5] arr << 6 #=> [1, 2, 3, 4, 5, 6]
unshift
will add a new item to the beginning of an array.arr.unshift(0) #=> [0, 1, 2, 3, 4, 5, 6]
With
insert
, you can add a new element to an array at any position.arr.insert(3, "apple") #=> [0, 1, 2, "apple", 3, 4, 5, 6]
Using the
insert
method, you can also insert multiple values at once:arr.insert(3, "orange", "pear", "grapefruit") #=> [0, 1, 2, "orange", "pear", "grapefruit", "apple", 3, 4, 5, 6]
6. Removing Items from an Array
The method
pop
removes the last element in an array and returns it:arr = [1, 2, 3, 4, 5, 6] arr.pop #=> 6 arr #=> [1, 2, 3, 4, 5]
To retrieve and at the same time remove the first item, use
shift
:arr.shift #=> 1 arr #=> [2, 3, 4, 5]
To delete an element at a particular index:
arr.delete_at(2) #=> 4 arr #=> [2, 3, 5]
To delete a particular element anywhere in an array, use
delete
:arr = [1, 2, 2, 3] arr.delete(2) #=> 2 arr #=> [1,3]
A useful method if you need to remove
nil
values from an array is compact
:arr = ["foo", 0, nil, "bar", 7, "baz", nil] arr.compact #=> ["foo", 0, "bar", 7, "baz"] arr #=> ["foo", 0, nil, "bar", 7, "baz", nil] arr.compact! #=> ["foo", 0, "bar", 7, "baz"] arr #=> ["foo", 0, "bar", 7, "baz"]
Another common need is to remove duplicate elements from an array.
It has the non-destructive
uniq
, and destructive method uniq!
arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556] arr.uniq #=> [2, 5, 6, 556, 8, 9, 0, 123]
7. Iterating over Arrays
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.Note that this operation leaves the array unchanged.
arr = [1, 2, 3, 4, 5] arr.each{|a| print a -= 10, " "} # prints: -9 -8 -7 -6 -5 #=> [1, 2, 3, 4, 5]
Another sometimes useful iterator is
reverse_each
which will iterate over the elements in the array in reverse order.words = %w[first second third fourth fifth sixth] str = "" words.reverse_each{|word| str += "\#\{word\} "} p str #=> "sixth fifth fourth third second first "
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:arr.map{|a| 2*a} #=> [2, 4, 6, 8, 10] arr #=> [1, 2, 3, 4, 5] arr.map!{|a| a**2} #=> [1, 4, 9, 16, 25] arr #=> [1, 4, 9, 16, 25]
8. Selecting Items from an Array
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.
8.1. Non-destructive Selection
arr = [1, 2, 3, 4, 5, 6] arr.select{|a| a > 3} #=> [4, 5, 6] arr.reject{|a| a < 3} #=> [3, 4, 5, 6] arr.drop_while{|a| a < 4} #=> [4, 5, 6] arr #=> [1, 2, 3, 4, 5, 6]
8.2. Destructive Selection
select!
and reject!
are the corresponding destructive methods to select
and reject
Similar to
select
vs. reject
, delete_if
and keep_if
have the exact opposite result when supplied with the same block:arr.delete_if{|a| a < 4} #=> [4, 5, 6] arr #=> [4, 5, 6] arr = [1, 2, 3, 4, 5, 6] arr.keep_if{|a| a < 4} #=> [1, 2, 3] arr #=> [1, 2, 3]
public class Array
Enumerable
┊
Array
Kernel
┊
Object
BasicObject
<<Array
<<Object
<<BasicObject
Class
Module
Kernel
┊
Object
BasicObject
public 1 Array.[]
Implementation candidates
[]
Usage
(*args)
→ ArrayReturns a new array populated with the given objects.
Array.[](1, "a", /^A/) # => [1, "a", /^A/] Array[1, "a", /^A/] # => [1, "a", /^A/] [1, "a", /^A/] # => [1, "a", /^A/]
Test 1. ruby/test/ruby/test_array.rb:test_01_square_brackets
1
Array[5, 4, 3, 2, 1].instance_of?(Array)
2
RETURN.length == 5
3
RETURN[0] == 5
4
RETURN[1] == 4
5
RETURN[2] == 3
6
RETURN[3] == 2
7
RETURN[4] == 1
8
RETURN[6].nil?
public 2 Array.new Misplaced Defined on Class
.
Implementation candidates
new
Returns a new array.
Usage (size = 0, obj = nil)
→ ArrayIf no arguments are sent, the new array will be empty. When a
Usage 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
.(array)
→ ArrayCreates a copy of the array passed as a parameter (the array is generated by calling
#to_ary
on the parameter).first_array = ["Matz", "Guido"] second_array = Array.new(first_array) #=> ["Matz", "Guido"] first_array.equal? second_array #=> false
(size){|index| block}
→ ArrayAn 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.Array.new(3){|index| index ** 2} # => [0, 1, 4]
Test 2. ruby/test/ruby/test_array.rb:test_00_new
9
Array.new.instance_of?(Array)
10
RETURN.empty?
11
RETURN[0].nil?
Common gotchas
When sending the second parameter, the same object will be used as the value for all the array elements:
a = Array.new(2, Hash.new) # => [{}, {}] a[0]["cat"] = "feline" a # => [{"cat"=>"feline"}, {"cat"=>"feline"}] a[1]["cat"] = "Felix" a # => [{"cat"=>"Felix"}, {"cat"=>"Felix"}]
Since all the
Array
elements store the same hash, changes to one of them will affect them all.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:
a = Array.new(2){Hash.new} a[0]["cat"] = "feline" a # => [{"cat"=>"feline"}, {}]
public 3 Array.try_convert
Implementation candidates
try_convert
Usage
(obj)
→ Array | nil
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.Array.try_convert([1]) #=> [1] Array.try_convert("1") #=> nil if tmp = Array.try_convert(arg) # the argument is an array elsif tmp = String.try_convert(arg) # the argument is a string end
Test 3. ruby/test/ruby/test_array.rb:test_try_convert
12
Array.try_convert([1]) == [1]
13
Array.try_convert("1").nil?
public 1 Array#&
Implementation candidates
&
See also
Usage [#uniq]
.(other_ary)
→ ArraySet Intersection — Returns a new array containing elements common to the two arrays, excluding any duplicates. The order is preserved from the original array.
It compares elements using their
hash
and eql?
methods for efficiency.[1, 1, 3, 5] & [1, 2, 3] #=> [1, 3] ["a", "b", "b", "z"] & ["a", "b", "c"] #=> ["a", "b"]
Test 4. ruby/test/ruby/test_array.rb:test_AND # '&'
14
([1, 1, 3, 5] & [1, 2, 3]) == [1, 3]
15
([1, 1, 3, 5] & []) == []
16
([] & [1, 2, 3]) == []
17
([1, 2, 3] & [4, 5, 6]) == []
Test 5. ruby/test/ruby/test_array.rb:ruby/test/ruby/test_array_andor_0:1/2
18
([1, 2, 3] & [2, 4, 6]) == [2]
Test 6. ruby/test/ruby/test_array.rb:test_0_literal:4/8
19
([1, 2, 3] & [2, 3, 4]) == [2, 3]
public 2 Array#*
Implementation candidates
*
Repetition
Usage (str)
→ StringEquivalent to
Usage self.join(str).
(int)
→ ArrayReturns a new array built by concatenating the
int
copies of self
.[1, 2, 3] * 3 #=> [1, 2, 3, 1, 2, 3, 1, 2, 3] [1, 2, 3] * "," #=> "1,2,3"
Test 7. ruby/test/ruby/test_array.rb:test_MUL # '*'
20
([] * 3) == []
21
([1] * 3) == [1, 1, 1]
22
([1, 2] * 3) == [1, 2, 1, 2, 1, 2]
23
([1, 2, 3] * 0) == []
24
([1, 2] * -3).raise?(ArgumentError)
25
([1, 2, 3, 4, 5] * "-") == "1-2-3-4-5"
26
([1, 2, 3, 4, 5] * "") == "12345"
Test 8. ruby/test/ruby/test_array.rb:test_times
Setup code
longp = [127, 63, 31, 15, 7].map{|x| 2**x-1}.find do |x| begin [].first(x) rescue ArgumentError true rescue RangeError false end end
27
([0, 0, 0, 0] * (longp + 1) / 4).raise?(ArgumentError)
Test 9. ruby/test/ruby/test_array.rb:test_0_literal:2/8
28
([1, 2] * 2) == [1, 2, 1, 2]
29
([1, 2] * ":") == "1:2"
public 3 Array#+
Implementation candidates
+
See also
Usage #concat
.(other_ary)
→ ArrayConcatenation — Returns a new array built by concatenating the two arrays together to produce a third array.
[1, 2, 3] + [4, 5] #=> [1, 2, 3, 4, 5] a = ["a", "b", "c"] c = a + ["d", "e", "f"] c #=> ["a", "b", "c", "d", "e", "f"] a #=> ["a", "b", "c"]
Test 10. ruby/test/ruby/test_array.rb:test_PLUS # '+'
30
([] + []) == []
31
([1] + []) == [1]
32
([] + [1]) == [1]
33
([1] + [1]) == [1, 1]
34
(["cat", "dog"] + [1, 2, 3]) == ["cat", "dog", 1, 2, 3]
Test 11. ruby/test/ruby/test_array.rb:test_0_literal:1/8
35
([1, 2] + [3, 4]) == [1, 2, 3, 4]
public 4 Array#-
Implementation candidates
-
Usage
(other_ary)
→ ArrayArray Difference
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.It compares elements using their
hash
and eql?
methods for efficiency.[1, 1, 2, 2, 3, 3, 4, 5] - [1, 2, 4] #=> [3, 3, 5]
If you need set-like behavior, see the library class
Set
.Test 12. ruby/test/ruby/test_array.rb:test_MINUS # '-'
36
([1] - [1]) == []
37
([1, 2, 3, 4, 5] - [2, 3, 4, 5]) == [1]
38
([1, 2, 1, 3, 1, 4, 1, 5] - [2, 3, 4, 5]) == [1, 1, 1, 1]
39
(1000.times.with_object(Array[]){|_, a| a << 1} - [2]) == Array[1] * 1000
40
([1, 2, 1] - [2]) == [1, 1]
41
([1, 2, 3] - [4, 5, 6]) == [1, 2, 3]
Test 13. ruby/test/ruby/test_array.rb:test_0_literal:6/8
42
([1, 2, 3] - [2, 3]) == [1]
public 5 Array#<<
Implementation candidates
<<
Usage
(obj)
→ ArrayAppend—Pushes the given object on to the end of this array. This expression returns the array itself, so several appends may be chained together.
[1, 2] << "c" << "d" << [3, 4] #=> [1, 2, "c", "d", [3, 4]]
Test 14. ruby/test/ruby/test_array.rb:test_LSHIFT # '<<'
Setup code
a = Array[]
43
(a << 1).succeed?
44
RECEIVER == [1]
45
((a << 1) << 2 << 3).succeed?
46
RECEIVER == [1, 2, 3]
47
((((a << 1) << 2) << 3) << nil << "cat").succeed?
48
RECEIVER == [1, 2, 3, nil, "cat"]
49
(((((a << 1) << 2) << 3) << nil) << "cat" << a).succeed?
50
RECEIVER == Array[1, 2, 3, nil, "cat", a]
public 6 Array#<=>
Implementation candidates
<=>
Usage
(other_ary)
→ -1
| 0
| 1
| nil
Comparison — Returns an integer (
-1
, 0
, or +1
) if this array is less than, equal to, or greater than other_ary
.nil
is returned if the two values are incomparable.Each object in each array is compared (using the
<=>
operator).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.
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.["a", "a", "c"] <=> ["a", "b", "c"] #=> -1 [1, 2, 3, 4, 5, 6] <=> [1, 2] #=> +1
Test 15. ruby/test/ruby/test_array.rb:test_CMP # '<=>'
51
([] <=> []).zero?
52
([1] <=> [1]).zero?
53
([1, 2, 3, "cat"] <=> [1, 2, 3, "cat"]).zero?
54
([] <=> [1]) == -1
55
([1] <=> []) == 1
56
([1, 2, 3] <=> [1, 2, 3, "cat"]) == -1
57
([1, 2, 3, "cat"] <=> [1, 2, 3]) == 1
58
([1, 2, 3, "cat"] <=> [1, 2, 3, "dog"]) == -1
59
([1, 2, 3, "dog"] <=> [1, 2, 3, "cat"]) == 1
public 7 Array#==
Implementation candidates
==
Usage
(other_ary)
→ true
| false
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
.["a", "c"] == ["a", "c", 7] #=> false ["a", "c", 7] == ["a", "c", 7] #=> true ["a", "c", 7] == ["a", "d", "f"] #=> false
Test 16. ruby/test/ruby/test_array.rb:test_EQUAL # '=='
60
([] == []) == true
61
([1] == [1]) == true
62
([1, 1, 2, 2] == [1, 1, 2, 2]) == true
63
([1, 1, 2, 2] == [1.0, 1.0, 2.0, 2.0]) == true
public 8 Array#=== Misplaced Defined on Kernel
.
Implementation candidates
===
Test 17. ruby/test/ruby/test_array.rb:test_VERY_EQUAL # '==='
64
([] === []) == true
65
([1] === [1]) == true
66
([1, 1, 2, 2] === [1, 1, 2, 2]) == true
67
([1, 1, 2, 2] === [1.0, 1.0, 2.0, 2.0]) == true
Missing Doc No paragraph given for user.
public 9 Array#[] (alias: slice)
Implementation candidates
[]
Usage
(index)
→ Object | nil
Usage (start, length)
→ Array | nil
Usage (range)
→ Array | nil
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.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.Returns
nil
if the index (or starting index) are out of range.a = ["a", "b", "c", "d", "e"] a[2] + a[0] + a[1] #=> "cab" a[6] #=> nil a[1, 2] #=> ["b", "c"] a[1..3] #=> ["b", "c", "d"] a[4..7] #=> ["e"] a[6..10] #=> nil a[-3, 3] #=> ["c", "d", "e"] # special cases a[5] #=> nil a[6, 1] #=> nil a[5, 1] #=> [] a[5..10] #=> []
Test 18. ruby/test/ruby/test_array.rb:test_AREF # '[]'
Setup code
a = Array[*(1..100).to_a]
68
a[0] == 1
69
a[99] == 100
70
a[100].nil?
71
a[-1] == 100
72
a[-2] == 99
73
a[-100] == 1
74
a[-101].nil?
75
a[-101, 0].nil?
76
a[-101, 1].nil?
77
a[-101, -1].nil?
78
a[10, -1].nil?
79
a[0, 1] == [1]
80
a[99, 1] == [100]
81
a[100, 1] == []
82
a[99, 100] == [100]
83
a[-1, 1] == [100]
84
a[-2, 1] == [99]
85
a[-100, 0] == []
86
a[-100, 1] == [1]
87
a[9, 3] == [10, 11, 12]
88
a[-91, 3] == [10, 11, 12]
89
a[0..0] == [1]
90
a[99..99] == [100]
91
a[100..100] == []
92
a[99..200] == [100]
93
a[-1..-1] == [100]
94
a[-2..-2] == [99]
95
a[9..11] == [10, 11, 12]
96
a[-91..-89] == [10, 11, 12]
97
a[10, -3].nil?
98
a[10..7] == []
99
a["cat"].raise?(TypeError)
Test 19. ruby/test/ruby/test_array.rb:test_aref
100
[][0, 0, 0].raise?(ArgumentError)
Test 20. ruby/test/ruby/test_array.rb:test_0_literal:7/8
Setup code
x = [0, 1, 2, 3, 4, 5]
101
x[2] == 2
102
x[1..3] == [1, 2, 3]
103
x[1, 3] == [1, 2, 3]
Test 21. ruby/test/ruby/test_array.rb:test_slice_frozen_array
Setup code
a = [1,2,3,4,5]
104
a[0, 4] == [1, 2, 3, 4]
105
a[1, 4] == [2, 3, 4, 5]
public 10 Array#[]=
Implementation candidates
[]=
See also
Usage #push
, and #unshift
.(index, obj)
→ ObjectUsage ((start, length), obj)
→ ObjectUsage (range, obj)
→ ObjectElement 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.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.Negative indices will count backward from the end of the array. For
start
and range
cases, the starting index is just before an element.An
IndexError
is raised if a negative index points past the beginning of the array.a = Array.new a[4] = "4"; #=> [nil, nil, nil, nil, "4"] a[0, 3] = ["a", "b", "c"] #=> ["a", "b", "c", nil, "4"] a[1..2] = [1, 2] #=> ["a", 1, 2, nil, "4"] a[0, 2] = "?" #=> ["?", 2, nil, "4"] a[0..2] = "A" #=> ["A", "4"] a[-1] = "Z" #=> ["A", "Z"] a[1..-1] = nil #=> ["A", nil] a[1..-1] = [] #=> ["A"] a[0, 0] = [1, 2] #=> [1, 2, "A"] a[3, 0] = "B" #=> [1, 2, "A", "B"]
Test 22. ruby/test/ruby/test_array.rb:test_ASET # '[]='
106
(Array[*(0..99).to_a][0] = 0).zero?
107
RECEIVER == Array[0] + Array[*(1..99).to_a]
108
(Array[*(0..99).to_a][10, 10] = 0).zero?
109
RECEIVER == Array[*(0..9).to_a] + Array[0] + Array[*(20..99).to_a]
110
(Array[*(0..99).to_a][-1] = 0).zero?
111
RECEIVER == Array[*(0..98).to_a] + Array[0]
112
(Array[*(0..99).to_a][-10, 10] = 0).zero?
113
RECEIVER == Array[*(0..89).to_a] + Array[0]
114
(Array[*(0..99).to_a][0, 1000] = 0).zero?
115
RECEIVER == [0]
116
(Array[*(0..99).to_a][10..19] = 0).zero?
117
RECEIVER == Array[*(0..9).to_a] + Array[0] + Array[*(20..99).to_a]
Setup code
b = Array[*%w(a b c)]
118
(Array[*(0..99).to_a][0, 1] = b) == b
119
RECEIVER == b + Array[*(1..99).to_a]
120
(Array[*(0..99).to_a][10, 10] = b) == b
121
RECEIVER == Array[*(0..9).to_a] + b + Array[*(20..99).to_a]
122
(Array[*(0..99).to_a][-1, 1] = b) == b
123
RECEIVER == Array[*(0..98).to_a] + b
124
(Array[*(0..99).to_a][-10, 10] = b) == b
125
RECEIVER == Array[*(0..89).to_a] + b
126
(Array[*(0..99).to_a][0, 1000] = b) == b
127
RECEIVER == b
128
(Array[*(0..99).to_a][10..19] = b) == b
129
RECEIVER == Array[*(0..9).to_a] + b + Array[*(20..99).to_a]
Setup code
a = Array[1, 2, 3]
130
(a[1, 0] = a).succeed?
131
RECEIVER == [1, 1, 2, 3, 2, 3]
Setup code
a = Array[1, 2, 3]
132
(a[-1, 0] = a).succeed?
133
RECEIVER == [1, 2, 1, 2, 3, 3]
134
([][5, 0] = [5]).succeed?
135
RECEIVER == [nil, nil, nil, nil, nil, 5]
136
([1][1, 0] = [2]).succeed?
137
RECEIVER == [1, 2]
138
([1][1, 1] = [2]).succeed?
139
RECEIVER == [1, 2]
Test 23. ruby/test/ruby/test_array.rb:test_splice
140
([0][-2, 0] = nil).raise?(IndexError)
Test 24. ruby/test/ruby/test_array.rb:test_0_literal:8/8
Setup code
x = [0, 1, 2, 3, 4, 5]
141
(x[0, 2] = 10).succeed?
142
RECEIVER == [10, 2, 3, 4, 5]
143
(x.tap{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:570)}[0, 0] = -1).succeed?
144
RECEIVER == [-1, 10, 2, 3, 4, 5]
145
(x[-1, 1] = 20).succeed?
146
RECEIVER[-1] == 20
147
RECEIVER.pop == 20
public 11 Array#assoc
Implementation candidates
assoc
See also
Usage #rassoc
.(obj)
→ Array | nil
Searches through an array whose elements are also arrays comparing
obj
with the first element of each contained array using obj.==
.Returns the first contained array that matches (that is, the first associated array), or
nil
if no match is found.s1 = ["colors", "red", "blue", "green"] s2 = ["letters", "a", "b", "c"] s3 = "foo" a = [s1, s2, s3] a.assoc("letters") #=> ["letters", "a", "b", "c"] a.assoc("foo") #=> nil
Test 25. ruby/test/ruby/test_array.rb:test_assoc
Setup code
a1 = Array[*%w(cat feline)] a2 = Array[*%w(dog canine)] a3 = Array[*%w(mule asinine)] a4 = Array[a1, a2, a3]
148
a4.assoc("cat") == a1
149
a4.assoc("mule") == a3
150
a4.assoc("asinine").nil?
151
a4.assoc("wombat").nil?
152
a4.assoc(1..2).nil?
public 12 Array#at
Implementation candidates
at
See also
Usage #[]
.(index)
→ Object | nil
Returns the element at
index
. A negative index counts from the end of self
. Returns nil
if the index is out of range.a = ["a", "b", "c", "d", "e"] a.at(0) #=> "a" a.at(-1) #=> "e"
Test 26. ruby/test/ruby/test_array.rb:test_at
Setup code
a = Array[*(0..99).to_a]
153
a.at(0).zero?
154
a.at(10) == 10
155
a.at(99) == 99
156
a.at(100).nil?
157
a.at(-1) == 99
158
a.at(-100).zero?
159
a.at(-101).nil?
160
a.at("cat").raise?(TypeError)
public 13 Array#bsearch
Implementation candidates
bsearch
Usage
{|x| block}
→ ObjectBy 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.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.
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:- the block returns
false
for any element whose index is less thani
, and - the block returns
true
for any element whose index is greater than or equal to i.
This method returns the
i
-th element. If i
is equal to ary.size
, it returns nil
.ary = [0, 4, 7, 10, 12] ary.bsearch{|x| x >= 4} #=> 4 ary.bsearch{|x| x >= 6} #=> 7 ary.bsearch{|x| x >= -1} #=> 0 ary.bsearch{|x| x >= 100} #=> nil
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:- the block returns a positive number for ary if 0 <= k < i,
- the block returns zero for ary if
i
<=k
<j
, and - the block returns a negative number for ary if
j
<=k
<ary.size
.
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
.ary = [0, 4, 7, 10, 12] # try to find v such that 4 <= v < 8 ary.bsearch{|x| 1 - x / 4} #=> 4 or 7 # try to find v such that 8 <= v < 10 ary.bsearch{|x| 4 - x / 2} #=> nil
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.Test 27. ruby/test/ruby/test_array.rb:test_bsearch_typechecks_return_values
161
[1, 2, 42, 100, 666].bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:651)}.raise?(TypeError)
162
[1, 2, 42, 100, 666].bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:652)} == nil
Test 28. ruby/test/ruby/test_array.rb:test_bsearch_with_no_block
163
[1, 2, 42, 100, 666].bsearch.succeed?
164
RETURN.size.nil?
165
RETURN.each{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:656)} == 42
Test 29. ruby/test/ruby/test_array.rb:test_bsearch_in_find_minimum_mode
Setup code
a = [0, 4, 7, 10, 12]
166
a.bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:659)} == 4
167
a.bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:660)} == 7
168
a.bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:661)}.zero?
169
a.bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:662)}.nil?
Test 30. ruby/test/ruby/test_array.rb:test_bsearch_in_find_any_mode
Setup code
a = [0, 4, 7, 10, 12]
170
a.bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:665)}.in?([4, 7])
171
a.bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:666)}.nil?
172
a.bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:667)}.nil?
173
a.bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:668)}.nil?
174
a.bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:669)}.in?([4, 7])
175
a.bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:670)}.nil?
176
a.bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:671)}.nil?
177
a.bsearch{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:672)}.in?([4, 7])
public 14 Array#clear
Implementation candidates
clear
Usage
→ ArrayRemoves all elements from
self
.a = ["a", "b", "c", "d", "e"] a.clear #=> []
Test 31. ruby/test/ruby/test_array.rb:test_clear2
178
([0] * 1024).clear == []
Test 32. ruby/test/ruby/test_array.rb:test_clear
Setup code
a = Array[1, 2, 3]
179
a.clear == []
180
RECEIVER == []
181
RECEIVER == a
public 15 Array#collect (alias: map)
Implementation candidates
collect
See also
Usage Enumerable#collect
.{|item| block}
→ ArrayInvokes the given block once for each element of
self
.Creates a new array containing the values returned by the block.
Usage
→ Enumeratora = ["a", "b", "c", "d"] a.collect{|x| x + "!"} #=> ["a!", "b!", "c!", "d!"] a.map.with_index{|x, i| x * i} #=> ["", "b", "cc", "ddd"] a #=> ["a", "b", "c", "d"]
Missing test Not unit tested.
public 16 Array#collect! (alias: map!)
Implementation candidates
collect!
See also
Usage Enumerable#collect
.{|item| block}
→ ArrayInvokes the given block once for each element of self, replacing the element with the value returned by the block.
Usage
→ Enumeratora = ["a", "b", "c", "d"] a.map!{|x| x + "!"} a #=> ["a!", "b!", "c!", "d!"] a.collect!.with_index{|x, i| x[0...i]} a #=> ["", "b", "c!", "d!"]
Missing test Not unit tested.
public 17 Array#combination
Implementation candidates
combination
Usage
(n){|c| block}
→ ArrayWhen invoked with a block, yields all combinations of length
n
of elements from the array and then returns the array itself.The implementation makes no guarantees about the order in which the combinations are yielded.
Usage (n)
→ EnumeratorExamples:
a = [1, 2, 3, 4] a.combination(1).to_a #=> [[1],[2],[3],[4]] a.combination(2).to_a #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]] a.combination(3).to_a #=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]] a.combination(4).to_a #=> [[1,2,3,4]] a.combination(0).to_a #=> [[]] # one combination of length 0 a.combination(5).to_a #=> [] # no combinations of length 5
Test 33. ruby/test/ruby/test_array.rb:test_combination
182
[1, 2, 3, 4].combination(0).to_a == [[]]
183
[1, 2, 3, 4].combination(1).to_a == [[1], [2], [3], [4]]
184
[1, 2, 3, 4].combination(2).to_a == [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
185
[1, 2, 3, 4].combination(3).to_a == [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
186
[1, 2, 3, 4].combination(4).to_a == [[1, 2, 3, 4]]
187
[1, 2, 3, 4].combination(5).to_a == []
Test 34. ruby/test/ruby/test_array.rb:test_combination2
Test 34.1. ruby-core:29240 Must be yielded even if 100C50 > signed integer
1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100].combination(50){(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:739)} == :called
combination
: LocalJumpError. Break from proc-closure.spec:739
public 18 Array#compact
Implementation candidates
compact
Usage
→ ArrayReturns a copy of
self
with all nil
elements removed.["a", nil, "b", nil, "c", nil].compact #=> ["a", "b", "c"]
Test 35. ruby/test/ruby/test_array.rb:test_compact_0:1/2
188
[nil, 1, nil, nil, 5, nil, nil].compact == [1, 5]
189
RECEIVER == [nil, 1, nil, nil, 5, nil, nil]
Test 36. ruby/test/ruby/test_array.rb:test_compact
190
[1, nil, nil, 2, 3, nil, 4].compact == [1, 2, 3, 4]
191
[nil, 1, nil, 2, 3, nil, 4].compact == [1, 2, 3, 4]
192
[1, nil, nil, 2, 3, nil, 4, nil].compact == [1, 2, 3, 4]
193
[1, 2, 3, 4].compact == [1, 2, 3, 4]
public 19 Array#compact!
Implementation candidates
compact!
Usage
→ Array | nil
Removes
nil
elements from the array.Returns
nil
if no changes were made, otherwise returns the array.["a", nil, "b", nil, "c"].compact! #=> ["a", "b", "c"] ["a", "b", "c"].compact! #=> nil
Test 37. ruby/test/ruby/test_array.rb:test_compact!
194
[1, nil, nil, 2, 3, nil, 4].compact! == [1, 2, 3, 4]
195
RECEIVER == [1, 2, 3, 4]
196
[nil, 1, nil, 2, 3, nil, 4].compact! == [1, 2, 3, 4]
197
RECEIVER == [1, 2, 3, 4]
198
[1, nil, nil, 2, 3, nil, 4, nil].compact! == [1, 2, 3, 4]
199
RECEIVER == [1, 2, 3, 4]
200
[1, 2, 3, 4].compact!.nil?
201
RECEIVER == [1, 2, 3, 4]
Test 38. ruby/test/ruby/test_array.rb:test_compact_0:2/2
202
[nil, 1, nil, nil, 5, nil, nil].compact!.succeed?
203
RECEIVER == [1, 5]
public 20 Array#concat
Implementation candidates
concat
See also
Usage Array#+
.(other_ary)
→ ArrayAppends the elements of
other_ary
to self
.["a", "b"].concat(["c", "d"]) #=> ["a", "b", "c", "d"] a = [1, 2, 3] a.concat([4, 5]) a #=> [1, 2, 3, 4, 5]
Test 39. ruby/test/ruby/test_array.rb:test_concat
204
[1, 2].concat([3, 4]) == [1, 2, 3, 4]
205
[].concat([1, 2, 3, 4]) == [1, 2, 3, 4]
206
[1, 2, 3, 4].concat([]) == [1, 2, 3, 4]
207
[].concat([]) == []
208
[[1, 2]].concat([[3, 4]]) == [[1, 2], [3, 4]]
Setup code
a = Array[1, 2, 3]
209
a.concat(a).succeed?
210
RECEIVER == [1, 2, 3, 1, 2, 3]
211
[0].concat(:foo).raise?(TypeError)
212
[0].concat(:foo).raise?(RuntimeError)
public 21 Array#count
Implementation candidates
count
Usage
(count)
→ IntegerReturns the number of elements.
Usage (obj)
→ IntegerCounts the number of elements which equal
Usage obj
using ==
.{|item| block}
→ IntegerCounts the number of elements for which the block returns a true value.
ary = [1, 2, 4, 2] ary.count #=> 4 ary.count(2) #=> 2 ary.count{|x| (x % 2).zero?} #=> 3
Missing test Not unit tested.
public 22 Array#cycle
Implementation candidates
cycle
Usage
(n=nil){|obj| block}
→ NilClassCalls the given block for each element
n
times or forever if nil
is given.Does nothing if a non-positive number is given or the array is empty.
Returns
Usage nil
if the loop has finished without getting interrupted.(n=nil)
→ Enumeratora = ["a", "b", "c"] a.cycle{|x| puts x} # print, a, b, c, a, b, c,.. forever. a.cycle(2){|x| puts x} # print, a, b, c, a, b, c.
Missing test Not unit tested.
public 23 Array#delete
Implementation candidates
delete
Usage
(obj)
→ Object | nil
Deletes all items from
self
that are equal to obj
.Returns the last deleted item, or
Usage nil
if no matching item is found.(obj){block}
→ ObjectThe result of the block is returned if the item is not found. (To remove
nil
elements and get an informative return value, use #compact!
)a = ["a", "b", "b", "b", "c"] a.delete("b") #=> "b" a #=> ["a", "c"] a.delete("z") #=> nil a.delete("z"){"not found"} #=> "not found"
Test 40. ruby/test/ruby/test_array.rb:test_delete
213
Array[*("cab".."cat").to_a].delete("cap") == "cap"
214
RECEIVER == Array[*("cab".."cao").to_a] + Array[*("caq".."cat").to_a]
215
Array[*("cab".."cat").to_a].delete("cab") == "cab"
216
RECEIVER == Array[*("cac".."cat").to_a]
217
Array[*("cab".."cat").to_a].delete("cat") == "cat"
218
RECEIVER == Array[*("cab".."cas").to_a]
219
Array[*("cab".."cat").to_a].delete("cup").nil?
220
RECEIVER == Array[*("cab".."cat").to_a]
221
Array[*("cab".."cat").to_a].delete("cup"){(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:848)} == 99
222
RECEIVER == Array[*("cab".."cat").to_a]
Setup code
o = Object.new def o.==(other); true; end o2 = Object.new def o2.==(other); true; end
223
Array[1, o, o2, 2].delete(42) == o2
224
RECEIVER == [1, 2]
public 24 Array#delete_at
Implementation candidates
delete_at
See also
Usage #slice!
(index)
→ Object | nil
Deletes the element at the specified
index
, returning that element, or nil
if the index is out of range.a = ["ant", "bat", "cat", "dog"] a.delete_at(2) #=> "cat" a #=> ["ant", "bat", "dog"] a.delete_at(99) #=> nil
Test 41. ruby/test/ruby/test_array.rb:test_delete_at
225
[1, 2, 3, 4, 5].delete_at(2) == 3
226
RECEIVER == [1, 2, 4, 5]
227
[1, 2, 3, 4, 5].delete_at(-2) == 4
228
RECEIVER == [1, 2, 3, 5]
229
[1, 2, 3, 4, 5].delete_at(5).nil?
230
RECEIVER == [1, 2, 3, 4, 5]
231
[1, 2, 3, 4, 5].delete_at(-6).nil?
232
RECEIVER == [1, 2, 3, 4, 5]
public 25 Array#delete_if
Implementation candidates
delete_if
See also
Usage #reject!
{|item| block}
→ ArrayDeletes every element of
self
for which block evaluates to true
.The array is changed instantly every time the block is called, not after the iteration is over.
Usage
→ Enumeratorscores = [97, 42, 75] scores.delete_if{|score| score < 80} #=> [97]
Test 42. ruby/test/ruby/test_array.rb:test_delete_if
Setup code
a = Array[1, 2, 3, 4, 5]
233
a.delete_if{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:891)} == a
234
RECEIVER == [1, 2, 3, 4, 5]
Setup code
a = Array[1, 2, 3, 4, 5]
235
a.delete_if{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:894)} == a
236
RECEIVER == []
Setup code
a = Array[1, 2, 3, 4, 5]
237
a.delete_if{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:897)} == a
238
RECEIVER == [1, 2, 3]
Test 42.1. bug2545 = ruby-core:27366
Setup code
a = Array[5, 6, 7, 8, 9, 10]
2
a.delete_if{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:901)} == 9
delete_if
: LocalJumpError. Break from proc-closure.spec:901
1
RECEIVER == [7, 8, 9, 10]
Missing a preceding successful unit test.
public 26 Array#drop
Implementation candidates
drop
See also
Usage #take
(n)
→ ArrayDrops first
n
elements from ary
and returns the rest of the elements in an array.If a negative number is given, raises an
ArgumentError
.a = [1, 2, 3, 4, 5, 0] a.drop(3) #=> [4, 5, 0]
Test 43. ruby/test/ruby/test_array.rb:test_drop
239
[1, 2, 3, 4, 5, 0].drop(3) == [4, 5, 0]
Test 43.1. ruby-dev:34123
240
[1, 2].drop(-1).raise?(ArgumentError)
Test 43.2. ruby-dev:34123
241
[1, 2].drop(1000000000) == []
public 27 Array#drop_while
Implementation candidates
drop_while
See also
Usage #take_while
{|arr| block}
→ ArrayDrops elements up to, but not including, the first element for which the block returns
Usage nil
or false
and returns an array containing the remaining elements.
→ Enumeratora = [1, 2, 3, 4, 5, 0] a.drop_while{|i| i < 3} #=> [3, 4, 5, 0]
Missing test Not unit tested.
public 28 Array#each
Implementation candidates
each
Usage
{|item| block}
→ ArrayCalls the given block once for each element in
Usage self
, passing that element as a parameter.
→ Enumeratora = ["a", "b", "c"] a.each{|x| print x, " -- "}
produces:
a -- b -- c --
Missing test Not unit tested.
public 29 Array#each_index
Implementation candidates
each_index
Usage
{|index| block}
→ ArraySame as
Usage #each
, but passes the index of the element instead of the element itself.
→ Enumeratora = ["a", "b", "c"] a.each_index{|x| print x, " -- "}
produces:
0 -- 1 -- 2 --
Missing test Not unit tested.
public 30 Array#empty?
Implementation candidates
empty?
Usage
→ true
| false
Returns
true
if self
contains no elements.[].empty? #=> true
Test 44. ruby/test/ruby/test_array.rb:test_empty_0
242
[].empty? == true
243
[1].empty? == false
244
[1, 1, 4, 2, 5, 4, 5, 1, 2].empty? == false
Test 45. ruby/test/ruby/test_array.rb:test_empty?
245
[].empty? == true
246
[1].empty? == false
public 31 Array#eql?
Implementation candidates
eql?
Usage
(other)
→ true
| false
Returns
true
if self
and other are the same object, or are both arrays with the same content (according to Object#eql?
).Test 46. ruby/test/ruby/test_array.rb:test_eql?
247
[].eql?([]) == true
248
[1].eql?([1]) == true
249
[1, 1, 2, 2].eql?([1, 1, 2, 2]) == true
250
[1, 1, 2, 2].eql?([1.0, 1.0, 2.0, 2.0]) == false
public 32 Array#fetch
Implementation candidates
fetch
Usage
(index)
→ ObjectTries to return the element at position
Usage index
, but throws an IndexError
exception if the referenced index
lies outside of the array bounds.(index, default)
→ ObjectThis error is prevented by the
Usage default
, which will act as a default value.(index){|index| block}
→ ObjectIt will only be executed when an invalid index is referenced. Negative values of index count from the end of the array.
a = [11, 22, 33, 44] a.fetch(1) #=> 22 a.fetch(-1) #=> 44 a.fetch(4, "cat") #=> "cat" a.fetch(100){|i| puts "\#\{i\} is out of bounds"} #=> "100 is out of bounds"
Test 47. ruby/test/ruby/test_array.rb:test_fetch
251
[].fetch(0, 0){(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:995)} == 1
252
[0, 1].fetch(-1) == 1
253
[0, 1].fetch(2).raise?(IndexError)
254
[0, 1].fetch(-3).raise?(IndexError)
255
[0, 1].fetch(2, 2) == 2
public 33 Array#fill
Implementation candidates
fill
Usage
(obj)
→ ArrayUsage (obj, start [, length])
→ ArrayUsage (obj, range)
→ ArraySets the selected elements of
self
(which may be the entire array) to obj
.A
start
of nil
is equivalent to zero.A
length
of nil
is equivalent to the length of the array.Negative values of
Usage start
count from the end of the array, where -1
is the last element.{|index| block}
→ ArrayUsage (start [, length]){|index| block}
→ ArrayUsage (range){|index| block}
→ ArrayFills the array with the value of the given block, which is passed the absolute index of each element to be filled.
a = ["a", "b", "c", "d"] a.fill("x") #=> ["x", "x", "x", "x"] a.fill("z", 2, 2) #=> ["x", "x", "z", "z"] a.fill("y", 0..1) #=> ["y", "y", "z", "z"] a.fill{|i| i*i} #=> [0, 1, 4, 9] a.fill(-2){|i| i*i*i} #=> [0, 1, 8, 27]
Test 48. ruby/test/ruby/test_array.rb:test_fill
256
[].fill(99) == []
257
[].fill(99, 0) == []
258
[].fill(99, 0, 1) == [99]
259
[].fill(99, 0..0) == [99]
260
[1].fill(99) == [99]
261
[1].fill(99, 0) == [99]
262
[1].fill(99, 0, 1) == [99]
263
[1].fill(99, 0..0) == [99]
264
[1, 2].fill(99) == [99, 99]
265
[1, 2].fill(99, 0) == [99, 99]
266
[1, 2].fill(99, nil) == [99, 99]
267
[1, 2].fill(99, 1, nil) == [1, 99]
268
[1, 2].fill(99, 0, 1) == [99, 2]
269
[1, 2].fill(99, 0..0) == [99, 2]
Test 49. ruby/test/ruby/test_array.rb:test_fill_0
270
[0, 1, 2, 3, 4, 5].fill(-1) == [-1, -1, -1, -1, -1, -1]
271
[0, 1, 2, 3, 4, 5].fill(-1, 3) == [0, 1, 2, -1, -1, -1]
272
[0, 1, 2, 3, 4, 5].fill(-1, 3, 2) == [0, 1, 2, -1, -1, 5]
273
[0, 1, 2, 3, 4, 5].fill(-1, 3, 5) == [0, 1, 2, -1, -1, -1, -1, -1]
274
[0, 1, 2, 3, 4, 5].fill(-1, 2, 2) == [0, 1, -1, -1, 4, 5]
275
[0, 1, 2, 3, 4, 5].fill(-1, 2, 5) == [0, 1, -1, -1, -1, -1, -1]
276
[0, 1, 2, 3, 4, 5].fill(-1, -2, 1) == [0, 1, 2, 3, -1, 5]
277
[0, 1, 2, 3, 4, 5].fill(-1, -2, 3) == [0, 1, 2, 3, -1, -1, -1]
278
[0, 1, 2, 3, 4, 5].fill(-1, 3..4) == [0, 1, 2, -1, -1, 5]
279
[0, 1, 2, 3, 4, 5].fill(-1, 3...4) == [0, 1, 2, -1, 4, 5]
280
[0, 1, 2, 3, 4, 5].fill(-1, 2..-2) == [0, 1, -1, -1, -1, 5]
281
[0, 1, 2, 3, 4, 5].fill(-1, 2...-2) == [0, 1, -1, -1, 4, 5]
282
[0, 1, 2, 3, 4, 5].fill{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1049)} == [10, 11, 12, 13, 14, 15]
283
[0, 1, 2, 3, 4, 5].fill(3){(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1050)} == [0, 1, 2, 13, 14, 15]
284
[0, 1, 2, 3, 4, 5].fill(3, 2){(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1051)} == [0, 1, 2, 13, 14, 5]
285
[0, 1, 2, 3, 4, 5].fill(3, 5){(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1052)} == [0, 1, 2, 13, 14, 15, 16, 17]
286
[0, 1, 2, 3, 4, 5].fill(3..4){(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1053)} == [0, 1, 2, 13, 14, 5]
287
[0, 1, 2, 3, 4, 5].fill(3...4){(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1054)} == [0, 1, 2, 13, 4, 5]
288
[0, 1, 2, 3, 4, 5].fill(2..-2){(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1055)} == [0, 1, 12, 13, 14, 5]
289
[0, 1, 2, 3, 4, 5].fill(2...-2){(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1056)} == [0, 1, 12, 13, 4, 5]
Test 50. ruby/test/ruby/test_array.rb:test_fill2
290
[].fill(0, 1, longp).raise?(ArgumentError)
public 34 Array#find_index (alias: index)
Implementation candidates
find_index
See also
Usage #rindex
.(obj)
→ Integer | nil
Returns the index of the first object in
Usage ary
such that the object is ==
to obj
. Returns nil
if no match is found.{|item| block}
→ Integer | nil
Returns the index of the first object for which the block returns
Usage true
. Returns nil
if no match is found.
→ Enumeratora = ["a", "b", "c"] a.index("b") #=> 1 a.index("z") #=> nil a.index{|x| x == "b"} #=> 1
Missing test Not unit tested.
public 35 Array#first
Implementation candidates
first
See also
Usage #last
for the opposite effect.first
→ Object | nil
Returns the first element of the array. If the array is empty, returns
Usage nil
.(n)
→ ArrayReturns the first
n
elements of the array. If the array is empty, returns an empty array.a = ["q", "r", "s", "t"] a.first #=> "q" a.first(2) #=> ["q", "r"]
Test 51. ruby/test/ruby/test_array.rb:test_first
291
[3, 4, 5].first == 3
292
[].first.nil?
Test 52. ruby/test/ruby/test_array.rb:test_first2
293
[0].first(2) == [0]
294
[0].first(-1).raise?(ArgumentError)
Test 53. ruby/test/ruby/test_array.rb:test_beg_end_0:1/6
Setup code
x = [1, 2, 3, 4, 5]
295
x.first == 1
296
x.first(1) == [1]
297
x.first(3) == [1, 2, 3]
public 36 Array#flatten
Implementation candidates
flatten
Usage
→ ArrayReturns a new array that is a one-dimensional flattening of
Usage self
(recursively). That is, for every element that is an array, extract its elements into the new array.(level)
→ ArrayThe optional
level
argument determines the level of recursion to flatten.s = [1, 2, 3] #=> [1, 2, 3] t = [4, 5, 6, [7, 8]] #=> [4, 5, 6, [7, 8]] a = [s, t, 9, 10] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10] a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = [1, 2, [3, [4, 5]]] a.flatten(1) #=> [1, 2, 3, [4, 5]]
Test 54. ruby/test/ruby/test_array.rb:test_flatten
Setup code
a1 = Array[1, 2, 3] a3 = Array[4, Array[5, 6]]
298
Array[a1, a3].flatten == [1, 2, 3, 4, 5, 6]
299
RECEIVER == Array[a1, a3]
300
Array[a1, Array[], a3].flatten == [1, 2, 3, 4, 5, 6]
301
[].flatten == []
302
[[[[], []], [[]], []], [[[]]]].flatten == []
Test 54.1. ruby-dev:31197
303
[[]].flatten("").raise?(TypeError)
Setup code
a6 = Array[[1, 2], 3] a6.taint a8 = Array[[1, 2], 3]
304
a6.flatten.tainted?
305
a8.flatten(0) == a8
306
RETURN.equal?(a8).!
public 37 Array#flatten!
Implementation candidates
flatten!
Usage
→ Array | nil
Flattens
Usage self
in place. Returns nil
if no modifications were made (i.e., the array contains no subarrays.(level)
→ Array | nil
The optional
level
argument determines the level of recursion to flatten.a = [1, 2, [3, [4, 5]]] a.flatten! #=> [1, 2, 3, 4, 5] a.flatten! #=> nil a #=> [1, 2, 3, 4, 5] a = [1, 2, [3, [4, 5]]] a.flatten!(1) #=> [1, 2, 3, [4, 5]]
Test 55. ruby/test/ruby/test_array.rb:test_flatten!
Setup code
a1 = Array[1, 2, 3] a3 = Array[4, Array[5, 6]] a5 = Array[a1, Array[], a3]
307
Array[a1, a3].flatten! == [1, 2, 3, 4, 5, 6]
308
RECEIVER == [1, 2, 3, 4, 5, 6]
Test 55.1. ruby-core:23382
309
a5.flatten! == [1, 2, 3, 4, 5, 6]
310
a5.flatten!(0).nil?
3
RECEIVER == [1, 2, 3, 4, 5, 6]
flatten!
: The receiver of verification==
is[[1, 2, 3], [], [4, [5, 6]]]
.
311
[].flatten!.nil?
312
[[[[], []], [[]], []], [[[]]]].flatten! == []
Test 55.2. ruby-core:23382
313
[].flatten!(0).nil?
public 38 Array#frozen?
Implementation candidates
frozen?
See also
Usage Object#frozen?
→ true
| false
Returns
true
if this array is frozen (or temporarily frozen while being sorted).Missing test Not unit tested.
public 39 Array#hash
Implementation candidates
hash
Usage
→ FixnumComputes a hash-code for this array. Two arrays with the same content will have the same hash code (and will compare using
eql?
).Test 56. ruby/test/ruby/test_array.rb:test_hash
314
["cat", "dog"].hash == Array["cat", "dog"].hash
4
["dog", "cat"].hash == Array["cat", "dog"].hash
hash
: The receiver of verification==
is-67794575767490625
.
Test 56.1. bug9231 = ruby-core:58993 Bug #9231
315
[].hash != false.hash
Test 57. ruby/test/ruby/test_array.rb:test_0_literal:3/8
316
[1, 2].hash == [1, 2].hash
public 40 Array#include?
Implementation candidates
include?
Usage
(object)
→ true
| false
Returns
true
if the given object is present in self
(that is, if any element ==
object
), otherwise returns false
.a = ["a", "b", "c"] a.include?("b") #=> true a.include?("z") #=> false
Test 58. ruby/test/ruby/test_array.rb:test_include?
Setup code
a = Array["cat", 99, /a/, Array[1, 2, 3]]
317
a.include?("cat") == true
318
a.include?(99) == true
319
a.include?(/a/) == true
320
a.include?([1, 2, 3]) == true
321
a.include?("ca") == false
322
a.include?([1, 2]) == false
public 41 Array#index
Implementation candidates
index
See also
Usage #rindex
.(obj)
→ Integer | nil
Returns the index of the first object in
Usage self
such that the object is ==
to obj
.{|item| block}
→ Integer | nil
Returns the index of the first object for which the block returns
Usage true
. Returns nil
if no match is found.index
→ Enumeratora = ["a", "b", "c"] a.index("b") #=> 1 a.index("z") #=> nil a.index{|x| x == "b"} #=> 1
Test 59. ruby/test/ruby/test_array.rb:test_index
Setup code
a = Array["cat", 99, /a/, 99, Array[1, 2, 3]]
323
a.index("cat").zero?
324
a.index(99) == 1
325
a.index([1, 2, 3]) == 4
326
a.index("ca").nil?
327
a.index([1, 2]).nil?
328
a.index(99){(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1216)} == 1
private 42 Array#initialize_copy
Implementation candidates
initialize_copy
Usage
(other_ary)
→ ArrayReplaces the contents of
self
with the contents of other_ary
, truncating or expanding if necessary.a = ["a", "b", "c", "d", "e"] a.replace(["x", "y", "z"]) #=> ["x", "y", "z"] a #=> ["x", "y", "z"]
Missing test Not unit tested.
public 43 Array#insert
Implementation candidates
insert
Usage
(index, obj...)
→ ArrayInserts the given values before the element with the given index.
Negative indices count backwards from the end of the array, where
-1
is the last element.a = %w{a b c d} a.insert(2, 99) #=> ["a", "b", 99, "c", "d"] a.insert(-2, 1, 2, 3) #=> ["a", "b", 99, "c", 1, 2, 3, "d"]
Missing test Not unit tested.
public 44 Array#inspect (alias: to_s)
Implementation candidates
inspect
Also aliased as:
Usage to_s
→ StringCreates a string representation of
self
.["a", "b", "c"].to_s #=> "[\"a\", \"b\", \"c\"]"
Missing test Not unit tested.
public 45 Array#join
Implementation candidates
join
Usage
(separator=$,)
→ StringReturns 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.["a", "b", "c"].join #=> "abc" ["a", "b", "c"].join("-") #=> "a-b-c"
Test 60. ruby/test/ruby/test_array.rb:test_join
Setup code
$, = "" a = Array[]
329
a.join == ""
330
a.join(",") == ""
331
a.join.encoding == #<Encoding:US-ASCII>
Setup code
$, = "" a = Array[1, 2]
332
a.join == "12"
333
a.join(nil) == "12"
334
a.join(",") == "1,2"
Setup code
$, = "" a = Array[1, 2, 3]
335
a.join == "123"
336
a.join(nil) == "123"
337
a.join(",") == "1,2,3"
Setup code
$, = ":" a = Array[1, 2, 3]
338
a.join == "1:2:3"
339
a.join(nil) == "1:2:3"
340
a.join(",") == "1,2,3"
Setup code
$, = "" a = Array[1, 2, 3] a.taint
341
a.join.tainted?
Test 60.1. bug5902 ruby-core:42161
Setup code
sep = ":".taint
342
[].join(sep).tainted? == false
343
[1].join(sep).tainted? == false
344
[1, 2].join(sep).tainted?
Setup code
e = "".force_encoding("EUC-JP") u = "".force_encoding("UTF-8")
345
[[]].join.encoding == #<Encoding:US-ASCII>
346
[1, [u]].join.encoding == #<Encoding:US-ASCII>
347
[u, [e]].join.encoding == #<Encoding:UTF-8>
348
[u, [1]].join.encoding == #<Encoding:UTF-8>
Test 60.2. bug5379 ruby-core:39776
349
[[], u, nil].join.encoding == #<Encoding:US-ASCII>
350
[[], "あ", nil].join.encoding == #<Encoding:UTF-8>
Setup code
$, = nil
public 46 Array#keep_if
Implementation candidates
keep_if
See also
Usage #select!
{|item| block}
→ ArrayDeletes every element of self for which the given block evaluates to
Usage false
.
→ Enumeratora = %w{a b c d e f} a.keep_if{|v| v =~ /[aeiou]/} #=> ["a", "e"]
Missing test Not unit tested.
public 47 Array#last
Implementation candidates
last
See also
Usage #first
for the opposite effect.
→ Object | nil
Returns the last element of
Usage self
. If the array is empty, returns nil
.(n)
→ ArrayReturns the last element(s) of
self
.a = ["w", "x", "y", "z"] a.last #=> "z" a.last(2) #=> ["y", "z"]
Test 61. ruby/test/ruby/test_array.rb:test_last
351
[].last.nil?
352
[1].last == 1
Setup code
a = Array[*(3..99).to_a]
353
a.last == 99
Test 62. ruby/test/ruby/test_array.rb:test_beg_end_0:2/6
Setup code
x = [1, 2, 3, 4, 5]
354
x.last == 5
355
x.last(1) == [5]
356
x.last(3) == [3, 4, 5]
public 48 Array#length (alias: size)
Implementation candidates
length
Also aliased as:
Usage size
→ IntegerReturns the number of elements in
self
. May be zero.[1, 2, 3, 4, 5].length #=> 5 [].length #=> 0
Test 63. ruby/test/ruby/test_array.rb:test_length
357
[].length.zero?
358
[1].length == 1
359
[1, nil].length == 2
360
[nil, 1].length == 2
361
Array[*(0..233).to_a].length == 234
public 49 Array#map
Implementation candidates
map
See also
Usage Enumerable#collect
.{|item| block}
→ ArrayInvokes the given block once for each element of
self
.Creates a new array containing the values returned by the block.
Usage
→ Enumeratora = ["a", "b", "c", "d"] a.collect{|x| x + "!"} #=> ["a!", "b!", "c!", "d!"] a.map.with_index{|x, i| x * i} #=> ["", "b", "cc", "ddd"] a #=> ["a", "b", "c", "d"]
Test 64. ruby/test/ruby/test_array.rb:test_collect
362
[1, "cat", 1..1].map{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1365)} == [Fixnum, String, Range]
363
[1, "cat", 1..1].map{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1366)} == [99, 99, 99]
364
[].map{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1367)} == []
365
[1, 2, 3].map.kind_of?(Enumerator)
public 50 Array#map!
Implementation candidates
map!
See also
Usage Enumerable#collect
.{|item| block}
→ ArrayInvokes the given block once for each element of
Usage self
, replacing the element with the value returned by the block.
→ Enumeratora = ["a", "b", "c", "d"] a.map!{|x| x + "!"} a #=> ["a!", "b!", "c!", "d!"] a.collect!.with_index{|x, i| x[0...i]} a #=> ["", "b", "c!", "d!"]
Test 65. ruby/test/ruby/test_array.rb:test_collect!
366
[1, "cat", 1..1].map!{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1383)} == [Fixnum, String, Range]
367
RECEIVER == [Fixnum, String, Range]
368
[1, "cat", 1..1].map!{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1385)} == [99, 99, 99]
369
RECEIVER == [99, 99, 99]
370
[].map!{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1387)} == []
371
RECEIVER == []
Test 66. ruby/test/ruby/test_array.rb:test_map!
372
[1, "cat", 1..1].map!{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1390)} == [Fixnum, String, Range]
373
RECEIVER == [Fixnum, String, Range]
374
[1, "cat", 1..1].map!{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1392)} == [99, 99, 99]
375
RECEIVER == [99, 99, 99]
376
[].map!{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1394)} == []
377
RECEIVER == []
public 51 Array#pack
Implementation candidates
pack
See also
Usage String#unpack
.(aTemplateString)
→ StringPacks 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.a = ["a", "b", "c"] n = [65, 66, 67] a.pack("A3A3A3") #=> "a b c " a.pack("a3a3a3") #=> "a\000\000b\000\000c\000\000" n.pack("ccc") #=> "ABC"
Directives for pack.
Integer | Array | Directive | Element | Meaning --------------------------------------------------------------------------- C | Integer | 8-bit unsigned (unsigned char) S | Integer | 16-bit unsigned, native endian (uint16_t) L | Integer | 32-bit unsigned, native endian (uint32_t) Q | Integer | 64-bit unsigned, native endian (uint64_t) | | c | Integer | 8-bit signed (signed char) s | Integer | 16-bit signed, native endian (int16_t) l | Integer | 32-bit signed, native endian (int32_t) q | Integer | 64-bit signed, native endian (int64_t) | | S_, S! | Integer | unsigned short, native endian I, I_, I! | Integer | unsigned int, native endian L_, L! | Integer | unsigned long, native endian Q_, Q! | Integer | unsigned long long, native endian (ArgumentError | | if the platform has no long long type.) | | (Q_ and Q! is available since Ruby 2.1.) | | s_, s! | Integer | signed short, native endian i, i_, i! | Integer | signed int, native endian l_, l! | Integer | signed long, native endian q_, q! | Integer | signed long long, native endian (ArgumentError | | if the platform has no long long type.) | | (q_ and q! is available since Ruby 2.1.) | | S> L> Q> | Integer | same as the directives without ">" except s> l> q> | | big endian S!> I!> | | (available since Ruby 1.9.3) L!> Q!> | | "S>" is same as "n" s!> i!> | | "L>" is same as "N" l!> q!> | | | | S< L< Q< | Integer | same as the directives without "<" except s< l< q< | | little endian S!< I!< | | (available since Ruby 1.9.3) L!< Q!< | | "S<" is same as "v" s!< i!< | | "L<" is same as "V" l!< q!< | | | | n | Integer | 16-bit unsigned, network (big-endian) byte order N | Integer | 32-bit unsigned, network (big-endian) byte order v | Integer | 16-bit unsigned, VAX (little-endian) byte order V | Integer | 32-bit unsigned, VAX (little-endian) byte order | | U | Integer | UTF-8 character w | Integer | BER-compressed integer Float | | Directive | | Meaning --------------------------------------------------------------------------- D, d | Float | double-precision, native format F, f | Float | single-precision, native format E | Float | double-precision, little-endian byte order e | Float | single-precision, little-endian byte order G | Float | double-precision, network (big-endian) byte order g | Float | single-precision, network (big-endian) byte order String | | Directive | | Meaning --------------------------------------------------------------------------- A | String | arbitrary binary string (space padded, count is width) a | String | arbitrary binary string (null padded, count is width) Z | String | same as ``a'', except that null is added with * B | String | bit string (MSB first) b | String | bit string (LSB first) H | String | hex string (high nibble first) h | String | hex string (low nibble first) u | String | UU-encoded string M | String | quoted printable, MIME encoding (see RFC2045) m | String | base64 encoded string (see RFC 2045, count is width) | | (if count is 0, no line feed are added, see RFC 4648) P | String | pointer to a structure (fixed-length string) p | String | pointer to a null-terminated string Misc. | | Directive | | Meaning --------------------------------------------------------------------------- @ | --- | moves to absolute position X | --- | back up a byte x | --- | null byte
Integer Directive | Array Element | Meaning |
C | Integer | 8-bit unsigned (unsigned char) |
S | Integer | 16-bit unsigned, native endian (uint16_t) |
L | Integer | 32-bit unsigned, native endian (uint32_t) |
Q | Integer | 64-bit unsigned, native endian (uint64_t) |
c | Integer | 8-bit signed (signed char) |
s | Integer | 16-bit signed, native endian (int16_t) |
l | Integer | 32-bit signed, native endian (int32_t) |
q | Integer | 64-bit signed, native endian (int64_t) |
S_, S! | Integer | unsigned short, native endian |
I, I_, I! | Integer | unsigned int, native endian |
L_, L! | Integer | unsigned long, native endian |
Q_, Q! | Integer | unsigned long long, native endian (ArgumentError if the platform has no long long type. Q_ and Q! are available since Ruby 2.1.) |
s_, s! | Integer | signed short, native endian |
i, i_, i! | Integer | signed int, native endian |
l_, l! | Integer | signed long, native endian |
q_, q! | Integer | signed long long, native endian (ArgumentError if the platform has no long long type. q_ and q! are available since Ruby 2.1.) |
S> L> Q> | Integer | same as the directives without ">" except |
s> l> q> | big endian |
Test 67. ruby/test/ruby/test_array.rb:test_pack
Setup code
a = Array[*%w(cat wombat x yy)]
378
a.pack("A3A3A3A3") == "catwomx yy "
379
a.pack("A*") == "cat"
380
a.pack("A3@1A3@2A3A3") == "cwx yy "
381
a.pack("a3a3a3a3") == "catwomx\u0000\u0000yy\u0000"
382
a.pack("a*") == "cat"
383
a.pack("a2") == "ca"
384
a.pack("a5") == "cat\u0000\u0000"
385
["01100001"].pack("B8") == "a"
386
["01100001"].pack("B*") == "a"
387
["0110000100110111"].pack("B8") == "a"
388
["0110000100110111"].pack("B16") == "a7"
389
["01100001", "00110111"].pack("B8B8") == "a7"
390
["01100001"].pack("B4") == "`"
391
["01100001"].pack("B2") == "@"
5
["01100001"].pack("b8") == "\x86"
pack
: The receiver of verification==
is"\x86"
.
6
["01100001"].pack("b*") == "\x86"
pack
: The receiver of verification==
is"\x86"
.
7
["0110000100110111"].pack("b8") == "\x86"
pack
: The receiver of verification==
is"\x86"
.
8
["0110000100110111"].pack("b16") == "\x86\xEC"
pack
: The receiver of verification==
is"\x86\xEC"
.
9
["01100001", "00110111"].pack("b8b8") == "\x86\xEC"
pack
: The receiver of verification==
is"\x86\xEC"
.
392
["01100001"].pack("b4") == "\u0006"
393
["01100001"].pack("b2") == "\u0002"
394
[65, 66, 67].pack("C3") == "ABC"
10
[-1, 66, 67].pack("C*") == "\xFFBC"
pack
: The receiver of verification==
is"\xFFBC"
.
395
[65, 66, 67].pack("c3") == "ABC"
11
[-1, 66, 67].pack("c*") == "\xFFBC"
pack
: The receiver of verification==
is"\xFFBC"
.
396
["4142", "0a", "12"].pack("H4H2H1") == "AB\n\u0010"
397
["1424", "a0", "21"].pack("h4h2h1") == "AB\n\u0002"
398
["abc\u0002def", "cat", "\u0001"].pack("M9M3M4") == "abc=02def=\ncat=\n=01=\n"
399
["hello\n"].pack("m") == "aGVsbG8K\n"
400
["hello\nhello\n"].pack("u") == ",:&5L;&\\*:&5L;&\\*\n"
401
[169, 66, 8800].pack("U*") == "©B≠"
Test 67.1. 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.Setup code
format = "c2x5CCxsdils_l_a6" ary = [1, -100, 127, 128, 32767, 987.654321098/100.0, 12345, 123456, -32767, -123456, "abcdef"]
12
ary.pack(format) !~ /def/
pack
: The receiver of verification!~
is"\x01\x9C\x00\x00\x00\x00\x00\x7F\x80\x00\xFF\x7F>a\x91E\xCA\xC0\#@90\x00\x00@\xE2\x01\x00\x01\x80\xC0\x1D\xFE\xFF\xFF\xFF\xFF\xFFabcdef"
.
402
RETURN.unpack(format).length == ary.length
403
RETURN.unpack(format).join(":") == ary.join(":")
public 52 Array#permutation
Implementation candidates
permutation
The implementation makes no guarantees about the order in which the permutations are yielded.
Usage (n){|p| block}
→ ArrayYields all permutations of length
Usage n
of the elements of the array, then return the array itself.{|p| block}
→ ArrayYields all permutations of all elements.
Usage (n)
→ EnumeratorUsage
→ EnumeratorExamples:
a = [1, 2, 3] a.permutation.to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] a.permutation(1).to_a #=> [[1],[2],[3]] a.permutation(2).to_a #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]] a.permutation(3).to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] a.permutation(0).to_a #=> [[]] # one permutation of length 0 a.permutation(4).to_a #=> [] # no permutations of length 4
Test 68. ruby/test/ruby/test_array.rb:test_permutation
Setup code
a = Array[1,2,3]
404
a.permutation(0).to_a == [[]]
405
a.permutation(1).to_a.sort == [[1], [2], [3]]
406
a.permutation(2).to_a.sort == [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
407
a.permutation(3).sort.to_a == [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
408
a.permutation(4).to_a == []
409
a.permutation(-1).to_a == []
Setup code
a = "edcba".each_char.to_a
410
a.permutation(5).sort == 'abcde'.each_char.to_a.permutation(5).sort
411
[].permutation(0).to_a == [[]]
Setup code
a = Array[1, 2, 3, 4] b = Array[]
13
a.permutation{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1649)} == [9, 8, 7, 6]
permutation
: The receiver of verification==
is[1, 2, 3, 4]
.
14
[1, 2, 3, 4].permutation.to_a == b
permutation
: The receiver of verification==
is[[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 3, 4, 2], [1, 4, 2, 3], [1, 4, 3, 2], [2, 1, 3, 4], [2, 1, 4, 3], [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1], [3, 1, 2, 4], [3, 1, 4, 2], [3, 2, 1, 4], [3, 2, 4, 1], [3, 4, 1, 2], [3, 4, 2, 1], [4, 1, 2, 3], [4, 1, 3, 2], [4, 2, 1, 3], [4, 2, 3, 1], [4, 3, 1, 2], [4, 3, 2, 1]]
.
Test 68.1. bug3708 ruby-dev:42067
15
[1, 2, 3, 4].permutation.to_a == b
permutation
: The receiver of verification==
is[[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 3, 4, 2], [1, 4, 2, 3], [1, 4, 3, 2], [2, 1, 3, 4], [2, 1, 4, 3], [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1], [3, 1, 2, 4], [3, 1, 4, 2], [3, 2, 1, 4], [3, 2, 4, 1], [3, 4, 1, 2], [3, 4, 2, 1], [4, 1, 2, 3], [4, 1, 3, 2], [4, 2, 1, 3], [4, 2, 3, 1], [4, 3, 1, 2], [4, 3, 2, 1]]
.
Test 69. bug9932 ruby-core:63103 Bug #9932
public 53 Array#pop
Implementation candidates
pop
See also
Usage #push
for the opposite effect.
→ Object | nil
Removes the last element from
Usage self
and returns it, or nil
if the array is empty.(n)
→ ArrayReturns an array of the last
n
elements (or less) just like array.slice!(-n, n)
does.a = ["a", "b", "c", "d"] a.pop #=> "d" a.pop(2) #=> ["b", "c"] a #=> ["a"]
Test 70. ruby/test/ruby/test_array.rb:test_pop
Setup code
a = Array["cat", "dog"]
412
a.pop == "dog"
413
RECEIVER == ["cat"]
16
a.pop == "cat"
pop
: The receiver of verification==
is"dog"
.
17
RECEIVER == []
pop
: The receiver of verification==
is["cat"]
.
18
a.pop.nil?
pop
: The receiver of verificationnil?
is"dog"
.
19
RECEIVER == []
pop
: The receiver of verification==
is["cat"]
.
Test 71. ruby/test/ruby/test_array.rb:test_beg_end_0:5/6
Setup code
x = [1, 2, 3, 4, 5]
414
x.pop == 5
20
x.pop(2) == [3, 4]
pop
: The receiver of verification==
is[4, 5]
.
21
RECEIVER == [1, 2]
pop
: The receiver of verification==
is[1, 2, 3]
.
public 54 Array#product
Implementation candidates
product
Usage
(other_ary, ...)
→ ArrayReturns an array of all combinations of elements from all arrays.
The length of the returned array is the product of the length of
Usage self
and the argument arrays.(other_ary, ...){|p| block}
→ ArrayProduct will yield all combinations and return
self
.[1,2,3].product([4,5]) #=> [[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]] [1,2].product([1,2]) #=> [[1,1],[1,2],[2,1],[2,2]] [1,2].product([3,4],[5,6]) #=> [[1,3,5],[1,3,6],[1,4,5],[1,4,6], # [2,3,5],[2,3,6],[2,4,5],[2,4,6]] [1,2].product() #=> [[1],[2]] [1,2].product([]) #=> []
Test 72. ruby/test/ruby/test_array.rb:test_product
415
[1, 2, 3].product([4, 5]) == [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]
416
[1, 2].product([1, 2]) == [[1, 1], [1, 2], [2, 1], [2, 2]]
417
[1, 2].product([3, 4], [5, 6]) == [[1, 3, 5], [1, 3, 6], [1, 4, 5], [1, 4, 6], [2, 3, 5], [2, 3, 6], [2, 4, 5], [2, 4, 6]]
418
[1, 2].product == [[1], [2]]
419
[1, 2].product([]) == []
Test 72.1. bug3394 = ruby-dev:41540
public 55 Array#push
Implementation candidates
push
See also
Usage #pop
for the opposite effect.(obj, ...)
→ ArrayAppend — 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.
a = ["a", "b", "c"] a.push("d", "e", "f") #=> ["a", "b", "c", "d", "e", "f"] [1, 2, 3,].push(4).push(5) #=> [1, 2, 3, 4, 5]
Test 73. ruby/test/ruby/test_array.rb:test_push
Setup code
a = Array[1, 2, 3]
420
a.push(4, 5) == [1, 2, 3, 4, 5]
22
a.push(nil) == [1, 2, 3, 4, 5, nil]
push
: The receiver of verification==
is[1, 2, 3, nil]
.
421
a.push.succeed?
23
RECEIVER == [1, 2, 3, 4, 5, nil]
push
: The receiver of verification==
is[1, 2, 3]
.
422
a.push(6, 7).succeed?
24
RECEIVER == [1, 2, 3, 4, 5, nil, 6, 7]
push
: The receiver of verification==
is[1, 2, 3, 6, 7]
.
Test 74. ruby/test/ruby/test_array.rb:test_beg_end_0:6/6
Setup code
x = [1, 2]
423
x.push(3, 4) == [1, 2, 3, 4]
25
x.push(5) == [1, 2, 3, 4, 5]
push
: The receiver of verification==
is[1, 2, 5]
.
26
RECEIVER == [1, 2, 3, 4, 5]
push
: The receiver of verification==
is[1, 2, 5]
.
public 56 Array#rassoc
Implementation candidates
rassoc
See also
Usage delete_if
.(obj)
→ Array | nil
Searches through the array whose elements are also arrays.
Compares
obj
with the second element of each contained array using obj.==
.Returns the first contained array that matches
obj
.a = [[1, "one"], [2, "two"], [3, "three"], ["ii", "two"]] a.rassoc("two") #=> [2, "two"] a.rassoc("four") #=> nil
Test 75. ruby/test/ruby/test_array.rb:test_rassoc
Setup code
a1 = Array[*%w(cat feline)] a2 = Array[*%w(dog canine)] a3 = Array[*%w(mule asinine)] a = Array[a1, a2, a3]
424
a.rassoc("feline") == a1
425
a.rassoc("asinine") == a3
426
a.rassoc("dog").nil?
427
a.rassoc("mule").nil?
428
a.rassoc(1..2).nil?
public 57 Array#reject
Implementation candidates
reject
See also
Usage #delete_if
{|item| block}
→ ArrayReturns a new array containing the items in
Usage self
for which the given block is not true.
→ EnumeratorTest 76. ruby/test/ruby/test_array.rb:test_reject
429
[0, 1, 2, 3].reject{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1778)} == [1, 3]
public 58 Array#reject!
Implementation candidates
reject!
See also
Usage Enumerable#reject
and #delete_if
.{|item| block}
→ ArrayEquivalent to
#delete_if
, deleting elements from self
for which the block evaluates to true
, but returns nil
if no changes were made.The array is changed instantly every time the block is called, not after the iteration is over.
Usage {|item| block}
→ NilClassUsage
→ EnumeratorTest 77. ruby/test/ruby/test_array.rb:test_reject!
430
[1, 2, 3, 4, 5].reject!{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1788)}.nil?
431
RECEIVER == [1, 2, 3, 4, 5]
Setup code
a = Array[1, 2, 3, 4, 5]
432
a.reject!{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1791)} == a
433
RECEIVER == []
Setup code
a = Array[1, 2, 3, 4, 5]
434
a.reject!{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1794)} == a
435
RECEIVER == [1, 2, 3]
Test 77.1. bug2545 ruby-core:27366
Setup code
a = Array[5, 6, 7, 8, 9, 10]
27
a.reject!{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:1798)} == 9
reject!
: LocalJumpError. Break from proc-closure.spec:1798
2
RECEIVER == [7, 8, 9, 10]
Missing a preceding successful unit test.
public 59 Array#repeated_combination
Implementation candidates
repeated_combination
Usage
(n){|c| block}
→ ArrayWhen invoked with a block, yields all repeated combinations of length
n
of elements from the array and then returns the array itself.The implementation makes no guarantees about the order in which the repeated combinations are yielded.
Usage (n)
→ EnumeratorIf no block is given, an
Enumerator
is returned instead.Examples:
a = [1, 2, 3] a.repeated_combination(1).to_a #=> [[1], [2], [3]] a.repeated_combination(2).to_a #=> [[1,1],[1,2],[1,3],[2,2],[2,3],[3,3]] a.repeated_combination(3).to_a #=> [[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3], # [1,3,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]] 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], # [1,1,3,3],[1,2,2,2],[1,2,2,3],[1,2,3,3],[1,3,3,3], # [2,2,2,2],[2,2,2,3],[2,2,3,3],[2,3,3,3],[3,3,3,3]] a.repeated_combination(0).to_a #=> [[]] # one combination of length 0
Missing test Not unit tested.
public 60 Array#repeated_permutation
Implementation candidates
repeated_permutation
Usage
(n){|p| block}
→ ArrayWhen invoked with a block, yield all repeated permutations of length
n
of the elements of the array, then return the array itself.The implementation makes no guarantees about the order in which the repeated permutations are yielded.
Usage (n)
→ EnumeratorExamples:
a = [1, 2] a.repeated_permutation(1).to_a #=> [[1], [2]] a.repeated_permutation(2).to_a #=> [[1,1],[1,2],[2,1],[2,2]] a.repeated_permutation(3).to_a #=> [[1,1,1],[1,1,2],[1,2,1],[1,2,2], # [2,1,1],[2,1,2],[2,2,1],[2,2,2]] a.repeated_permutation(0).to_a #=> [[]] # one permutation of length 0
Missing test Not unit tested.
public 61 Array#replace (alias: initialize_copy)
Implementation candidates
replace
Usage
(other_ary)
→ ArrayReplaces the contents of
self
with the contents of other_ary
, truncating or expanding if necessary.a = ["a", "b", "c", "d", "e"] a.replace(["x", "y", "z"]) #=> ["x", "y", "z"] a #=> ["x", "y", "z"]
Test 78. ruby/test/ruby/test_array.rb:test_replace_shared_ary
Setup code
a = [1] * 100 b = []
1
b.replace(a)
Missing a verification.
436
a.replace([1, 2, 3]).succeed?
437
RECEIVER == [1, 2, 3]
public 62 Array#reverse
Implementation candidates
reverse
Usage
→ ArrayReturns a new array containing
self
‘s elements in reverse order.["a", "b", "c"].reverse #=> ["c", "b", "a"] [1].reverse #=> [1]
Test 79. ruby/test/ruby/test_array.rb:test_reverse
438
["dog", "cat", "bee", "ant"].reverse == ["ant", "bee", "cat", "dog"]
439
RECEIVER == ["dog", "cat", "bee", "ant"]
440
[].reverse == []
public 63 Array#reverse!
Implementation candidates
reverse!
Usage
→ ArrayReverses
self
in place.a = ["a", "b", "c"] a.reverse! #=> ["c", "b", "a"] a #=> ["c", "b", "a"]
Test 80. ruby/test/ruby/test_array.rb:test_reverse!
441
["dog", "cat", "bee", "ant"].reverse! == ["ant", "bee", "cat", "dog"]
442
RECEIVER == ["ant", "bee", "cat", "dog"]
Test 80.1. Array#reverse always returns self.
443
[].reverse! == []
public 64 Array#reverse_each
Implementation candidates
reverse_each
Usage
{|item| block}
→ ArrayUsage
→ EnumeratorSame as
#each
, but traverses self
in reverse order.a = ["a", "b", "c"] a.reverse_each{|x| print x, " "}
produces:
c b a
Missing test Not unit tested.
public 65 Array#rindex
Implementation candidates
rindex
See also
Usage #index
.(obj)
→ Integer | nil
Returns the index of the last object in
Usage self
==
to obj
. Returns nil
if no match is found.{|item| block}
→ Integer | nil
Returns the index of the first object for which the block returns true, starting from the last object.
Usage
→ Enumeratora = ["a", "b", "b", "b", "c"] a.rindex("b") #=> 3 a.rindex("z") #=> nil a.rindex{|x| x == "b"} #=> 3
Missing test Not unit tested.
public 66 Array#rotate
Implementation candidates
rotate
Usage
(count=1)
→ ArrayReturns a new array by rotating
self
so that the element at count is the first element of the new array.If count is negative then it rotates in the opposite direction, starting from the end of
self
where -1
is the last element.a = ["a", "b", "c", "d"] a.rotate #=> ["b", "c", "d", "a"] a #=> ["a", "b", "c", "d"] a.rotate(2) #=> ["c", "d", "a", "b"] a.rotate(-3) #=> ["b", "c", "d", "a"]
Test 81. ruby/test/ruby/test_array.rb:test_rotate
Setup code
a = [1,2,3,4,5]
444
a.rotate == [2, 3, 4, 5, 1]
445
a.rotate(-1) == [5, 1, 2, 3, 4]
446
a.rotate(2) == [3, 4, 5, 1, 2]
447
a.rotate(-2) == [4, 5, 1, 2, 3]
448
a.rotate(13) == [4, 5, 1, 2, 3]
449
a.rotate(-13) == [3, 4, 5, 1, 2]
Setup code
a = [1]
450
a.rotate == [1]
451
a.rotate(2) == [1]
452
a.rotate(-4) == [1]
453
a.rotate(13) == [1]
454
a.rotate(-13) == [1]
Setup code
a = []
455
a.rotate == []
456
a.rotate(2) == []
457
a.rotate(-4) == []
458
a.rotate(13) == []
459
a.rotate(-13) == []
460
[1, 2, 3].rotate(1, 1).raise?(ArgumentError)
461
[1, 2, 3, 4, 5].rotate(2147483647.9) == [3, 4, 5, 1, 2]
462
[1, 2, 3, 4, 5].rotate(-2147483648.9) == [3, 4, 5, 1, 2]
public 67 Array#rotate!
Implementation candidates
rotate!
Usage
(count=1)
→ ArrayRotates
self
in place so that the element at count comes first, and returns self
.If count is negative, then it rotates in the opposite direction, starting from the end of the array where
-1
is the last element.a = ["a", "b", "c", "d"] a.rotate! #=> ["b", "c", "d", "a"] a #=> ["b", "c", "d", "a"] a.rotate!(2) #=> ["d", "a", "b", "c"] a.rotate!(-3) #=> ["a", "b", "c", "d"]
Test 82. ruby/test/ruby/test_array.rb:test_rotate!
Setup code
a = [1,2,3,4,5]
463
a.rotate! == [2, 3, 4, 5, 1]
464
RECEIVER == [2, 3, 4, 5, 1]
28
a.rotate!(2) == [4, 5, 1, 2, 3]
rotate!
: The receiver of verification==
is[3, 4, 5, 1, 2]
.
29
a.rotate!(-4) == [5, 1, 2, 3, 4]
rotate!
: The receiver of verification==
is[2, 3, 4, 5, 1]
.
30
a.rotate!(13) == [3, 4, 5, 1, 2]
rotate!
: The receiver of verification==
is[4, 5, 1, 2, 3]
.
31
a.rotate!(-13) == [5, 1, 2, 3, 4]
rotate!
: The receiver of verification==
is[3, 4, 5, 1, 2]
.
Setup code
a = [1]
465
a.rotate! == [1]
466
a.rotate!(2) == [1]
467
a.rotate!(-4) == [1]
468
a.rotate!(13) == [1]
469
a.rotate!(-13) == [1]
Setup code
a = []
470
a.rotate! == []
471
a.rotate!(2) == []
472
a.rotate!(-4) == []
473
a.rotate!(13) == []
474
a.rotate!(-13) == []
32
[].rotate!.raise?(RuntimeError, message: /can\'t modify frozen/)
rotate!
: Did not raise an exception.
475
[1, 2, 3].rotate!(1, 1).raise?(ArgumentError)
public 68 Array#sample
Implementation candidates
sample
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.
If the array is empty the first form returns
Usage nil
and the second form returns an empty array.
→ ObjectChooses a random element from the array. If the array is empty,returns
Usage nil
.(n)
→ ArrayChooses
Usage n
random elements from the array. If the array is empty, returns an empty array.(random: rng)
→ ObjectUsage (n, random: rng)
→ ArrayThe optional
rng
argument will be used as the random number generator.a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a.sample #=> 7 a.sample(4) #=> [6, 4, 2, 5]
Missing test Not unit tested.
public 69 Array#select
Implementation candidates
select
See also
Usage Enumerable#select
.{|item| block}
→ ArrayReturns a new array containing all elements of ary for which the given block returns a true value.
Usage
→ Enumerator[1,2,3,4,5].select{|num| num.even?} #=> [2, 4] a = %w{a b c d e f} a.select{|v| v =~ /[aeiou]/} #=> ["a", "e"]
Test 83. ruby/test/ruby/test_array.rb:test_find_all_0:1/2
476
[].select.succeed?
public 70 Array#select!
Implementation candidates
select!
See also
Usage #keep_if
{|item| block}
→ Array | nil
Invokes the given block passing in successive elements from
self
, deleting elements for which the block returns a false value.If changes were made, it will return
Usage self
, otherwise it returns nil
.
→ EnumeratorMissing test Not unit tested.
public 71 Array#shift
Implementation candidates
shift
See also
Usage #unshift
for the opposite effect.
→ Object | nil
Removes the first element of
Usage self
and returns it (shifting all other elements down by one). Returns nil
if the array is empty.(n)
→ ArrayReturns 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
.args = ["-m", "-q", "filename"] args.shift #=> "-m" args #=> ["-q", "filename"] args = ["-m", "-q", "filename"] args.shift(2) #=> ["-m", "-q"] args #=> ["filename"]
Test 84. ruby/test/ruby/test_array.rb:test_unshift_error
33
[].shift("cat").raise?(RuntimeError)
shift
: TypeError. No implicit conversion of String into Integer.
34
[].shift.raise?(RuntimeError)
shift
: Did not raise an exception.
Test 85. ruby/test/ruby/test_array.rb:test_beg_end_0:3/6
Setup code
x = [1, 2, 3, 4, 5]
477
x.shift == 1
35
x.shift(3) == [2, 3, 4]
shift
: The receiver of verification==
is[1, 2, 3]
.
36
RECEIVER == [5]
shift
: The receiver of verification==
is[4, 5]
.
public 72 Array#shuffle
Implementation candidates
shuffle
Usage
Usage
→ ArrayReturns a new array with elements of
self
shuffled.a = [1, 2, 3] #=> [1, 2, 3] a.shuffle #=> [2, 3, 1]
(random: rng)
→ ArrayThe optional
rng
argument will be used as the random number generator.a.shuffle(random: Random.new(1)) #=> [1, 3, 2]
Missing test Not unit tested.
public 73 Array#shuffle!
Implementation candidates
shuffle!
Usage
→ ArrayShuffles elements in
Usage self
in place.(random: rng)
→ ArrayThe optional
rng
argument will be used as the random number generator.Missing test Not unit tested.
public 74 Array#size Misplaced Defined as alias of length
.
Implementation candidates
size
Alias for:
length
Test 86. ruby/test/ruby/test_array.rb:test_size
478
[].size.zero?
479
[1].size == 1
480
Array[*(0..99).to_a].size == 100
Missing Doc No method signature given.
public 75 Array#slice
Implementation candidates
slice
Element Reference
Negative indices count backward from the end of the array (
-1
is the last element).Returns
Usage nil
if the index (or starting index) are out of range.(index)
→ Object | nil
Returns the element at
Usage index
.(start, length)
→ Array | nil
Usage (range)
→ Array | nil
Returns a subarray starting at the
start
index and continuing for length
elements, or a subarray specified by range
of indices.The starting index is just before an element.
An empty array is returned when the starting index for an element range is at the end of the array.
a = ["a", "b", "c", "d", "e"] a[2] + a[0] + a[1] #=> "cab" a[6] #=> nil a[1, 2] #=> ["b", "c"] a[1..3] #=> ["b", "c", "d"] a[4..7] #=> ["e"] a[6..10] #=> nil a[-3, 3] #=> ["c", "d", "e"] # special cases a[5] #=> nil a[6, 1] #=> nil a[5, 1] #=> [] a[5..10] #=> []
Missing test Not unit tested.
public 76 Array#slice!
Implementation candidates
slice!
Returns the deleted object (or objects), or
Usage nil
if the index is out of range.(index)
→ Object | nil
Deletes the element given by an index.
Usage (start, length)
→ Array | nil
Usage (range)
→ Array | nil
Deletes the element(s) given by an index up to
length
elements or by range
.a = ["a", "b", "c"] a.slice!(1) #=> "b" a #=> ["a", "c"] a.slice!(-1) #=> "c" a #=> ["a"] a.slice!(100) #=> nil a #=> ["a"]
Test 87. ruby/test/ruby/test_array.rb:test_slice!
481
[1, 2, 3, 4, 5].slice!(2) == 3
482
RECEIVER == [1, 2, 4, 5]
483
[1, 2, 3, 4, 5].slice!(-2) == 4
484
RECEIVER == [1, 2, 3, 5]
485
[1, 2, 3, 4, 5].slice!(2, 2) == [3, 4]
486
RECEIVER == [1, 2, 5]
487
[1, 2, 3, 4, 5].slice!(-2, 2) == [4, 5]
488
RECEIVER == [1, 2, 3]
489
[1, 2, 3, 4, 5].slice!(2..3) == [3, 4]
490
RECEIVER == [1, 2, 5]
491
[1, 2, 3, 4, 5].slice!(20).nil?
492
RECEIVER == [1, 2, 3, 4, 5]
493
[1, 2, 3, 4, 5].slice!(-6).nil?
494
RECEIVER == [1, 2, 3, 4, 5]
495
[1, 2, 3, 4, 5].slice!(-6..4).nil?
496
RECEIVER == [1, 2, 3, 4, 5]
497
[1, 2, 3, 4, 5].slice!(-6, 2).nil?
498
RECEIVER == [1, 2, 3, 4, 5]
499
[1].slice!.raise?(ArgumentError)
500
[1].slice!(0, 0, 0).raise?(ArgumentError)
public 77 Array#sort
Implementation candidates
sort
See also
Enumerable#sort_by
.Returns a new array created by sorting
Usage self
.
→ ArrayComparisons for the
Usage sort
will be done using the <=>
operator.{|a, b| block}
→ ArrayComparisons for the
sort
will be done using the code block.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
.a = ["d", "a", "e", "c", "b"] a.sort #=> ["a", "b", "c", "d", "e"] a.sort{|x,y| y <=> x} #=> ["e", "d", "c", "b", "a"]
Test 88. ruby/test/ruby/test_array.rb:test_sort_0:1/2
501
["it", "came", "to", "pass", "that", "..."].sort.join(" ") == "... came it pass that to"
public 78 Array#sort!
Implementation candidates
sort!
See also
Enumerable#sort_by
.Sorts
Usage self
in place.
→ ArrayComparisons for the
Usage sort
will be done using the <=>
operator.{|a, b| block}
→ ArrayComparisons for the
sort
will be done using the code block.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
.a = ["d", "a", "e", "c", "b"] a.sort! #=> ["a", "b", "c", "d", "e"] a.sort!{|x,y| y <=> x} #=> ["e", "d", "c", "b", "a"]
Test 89. ruby/test/ruby/test_array.rb:test_sort_0:2/2
Test 89.1. sort with condition
502
[2, 5, 3, 1, 7].sort!{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:2159)}.succeed?
503
RECEIVER == [1, 2, 3, 5, 7]
Test 89.2. reverse sort
504
[2, 5, 3, 1, 7].sort!{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:2162)}.succeed?
505
RECEIVER == [7, 5, 3, 2, 1]
public 79 Array#sort_by!
Implementation candidates
sort_by!
Usage
{|obj| block}
→ ArraySorts
Usage self
in place using a set of keys generated by mapping the values in self through the given block.
→ EnumeratorTest 90. ruby/test/ruby/test_array.rb:test_sort_by!
506
[1, 3, 5, 2, 4].sort_by!{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:2170)}.succeed?
507
RECEIVER == [5, 4, 3, 2, 1]
public 80 Array#take
Implementation candidates
take
See also
Usage #drop
(n)
→ ArrayReturns first
n
elements from the array.If a negative number is given, raises an
ArgumentError
.a = [1, 2, 3, 4, 5, 0] a.take(3) #=> [1, 2, 3]
Test 91. ruby/test/ruby/test_array.rb:test_take
508
[1, 2, 3, 4, 5, 0].take(3) == [1, 2, 3]
Test 91.1. ruby-dev:34123
509
[1, 2].take(-1).raise?(ArgumentError)
Test 91.2. ruby-dev:34123
510
[1, 2].take(1000000000) == [1, 2]
public 81 Array#take_while
Implementation candidates
take_while
See also
Usage #drop_while
{|arr| block}
→ ArrayPasses elements to the block until the block returns
Usage nil
or false
, then stops iterating and returns an array of all prior elements.
→ Enumeratora = [1, 2, 3, 4, 5, 0] a.take_while{|i| i < 3} #=> [1, 2]
Test 92. ruby/test/ruby/test_array.rb:test_take_while
511
[1, 2, 3, 4, 5, 0].take_while{(/home/sawa/Dropbox/implicit/gem/manager/examples/array/spec:2199)} == [1, 2]
public 82 Array#to_a
Implementation candidates
to_a
Usage
→ ArrayReturns
self
.If called on a subclass of
Array
, converts the receiver to an Array
object.Missing test Not unit tested.
public 83 Array#to_ary
Implementation candidates
to_ary
Usage
→ ArrayReturns
self
.Missing test Not unit tested.
public 84 Array#to_h
Implementation candidates
to_h
Usage
→ HashReturns the result of interpreting
ary
as an array of [key, value]
pairs.[[:foo, :bar], [1, 2]].to_h # => {:foo => :bar, 1 => 2}
Missing test Not unit tested.
public 85 Array#to_s Misplaced Defined as alias of inspect
.
Implementation candidates
to_s
Alias for:
inspect
.Test 93. ruby/test/ruby/test_array.rb:test_to_s
Setup code
$, = ""
512
[].to_s == "[]"
Setup code
$, = ""
513
[1, 2].to_s == "[1, 2]"
Setup code
$, = ""
514
[1, 2, 3].to_s == "[1, 2, 3]"
Setup code
$, = ":"
515
[1, 2, 3].to_s == "[1, 2, 3]"
Setup code
$, = nil
Teardown
Missing Doc No method signature given.
public 86 Array#transpose
Implementation candidates
transpose
Usage
→ ArrayAssumes that
self
is an array of arrays and transposes the rows and columns.a = [[1,2], [3,4], [5,6]] a.transpose #=> [[1, 3, 5], [2, 4, 6]]
If the length of the subarrays don’t match, an
IndexError
is raised.Test 94. ruby/test/ruby/test_array.rb:test_transpose
516
[[1, 2, 3], [:a, :b, :c]].transpose == [[1, :a], [2, :b], [3, :c]]
517
[[1, 2, 3], [:a, :b]].transpose.raise?(IndexError)
public 87 Array#uniq
Implementation candidates
uniq
Usage
→ ArrayReturns a new array by removing duplicate values in
self
.It compares values using their hash and eql? methods for efficiency.
Usage {|item| ...}
→ ArrayIt will use the return value of the block for comparison.
a = ["a", "a", "b", "b", "c"] a.uniq # => ["a", "b", "c"] b = [["student","sam"], ["student","george"], ["teacher","matz"]] b.uniq{|s| s.first} # => [["student", "sam"], ["teacher", "matz"]]
Missing test Not unit tested.
public 88 Array#uniq!
Implementation candidates
uniq!
Usage
→ Array | nil
Removes duplicate elements from
self
.It compares values using their
hash
and eql?
methods for efficiency.Returns
Usage nil
if no changes are made (that is, no duplicates are found).{|item| ...}
→ Array | nil
It will use the return value of the block for comparison.
a = ["a", "a", "b", "b", "c"] a.uniq! # => ["a", "b", "c"] b = ["a", "b", "c"] b.uniq! # => nil c = [["student","sam"], ["student","george"], ["teacher","matz"]] c.uniq!{|s| s.first} # => [["student", "sam"], ["teacher", "matz"]]
Test 95. ruby/test/ruby/test_array.rb:test_uniq_0
518
[1, 1, 4, 2, 5, 4, 5, 1, 2].uniq!.succeed?
519
RECEIVER == [1, 4, 2, 5]
public 89 Array#unshift
Implementation candidates
unshift
See also
Usage #shift
for the opposite effect.(obj, ...)
→ ArrayPrepends objects to the front of
self
, moving other elements upwards.a = ["b", "c", "d"] a.unshift("a") #=> ["a", "b", "c", "d"] a.unshift(1, 2) #=> [1, 2, "a", "b", "c", "d"]
Test 96. ruby/test/ruby/test_array.rb:test_beg_end_0:4/6
Setup code
x = [5]
520
x.unshift(2, 3, 4) == [2, 3, 4, 5]
37
x.unshift(1) == [1, 2, 3, 4, 5]
unshift
: The receiver of verification==
is[1, 5]
.
38
RECEIVER == [1, 2, 3, 4, 5]
unshift
: The receiver of verification==
is[1, 5]
.
public 90 Array#values_at
Implementation candidates
values_at
See also
Usage #select
.(selector, ...)
→ ArrayReturns an array containing the elements in
self
corresponding to the given selector(s).The selectors may be either integer indices or ranges.
a = %w{a b c d e f} a.values_at(1, 3, 5) # => ["b", "d", "f"] a.values_at(1, 3, 5, 7) # => ["b", "d", "f", nil] a.values_at(-1, -2, -2, -7) # => ["f", "e", "e", nil] a.values_at(4..6, 3...6) # => ["e", "f", nil, "d", "e", "f"]
Test 97. ruby/test/ruby/test_array.rb:test_values_at
Setup code
a = Array[*("a".."j").to_a]
521
a.values_at(0, 2, 4) == ["a", "c", "e"]
522
a.values_at(-1, -3, -5) == ["j", "h", "f"]
523
a.values_at(-3, 99, 0) == ["h", nil, "a"]
public 91 Array#zip
Implementation candidates
zip
Converts any arguments to arrays, then merges elements of self with corresponding elements from each argument.
This generates a sequence of
ary.size
n-element arrays, where n is one more than the count of arguments.If the size of any argument is less than the size of the initial array,
Usage nil
values are supplied.(arg, ...)
→ ArrayAn array of arrays is returned.
Usage (arg, ...){|arr| block}
→ NilClassIt is invoked for each output array.
a = [4, 5, 6] b = [7, 8, 9] [1, 2, 3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]] [1, 2].zip(a, b) #=> [[1, 4, 7], [2, 5, 8]] a.zip([1, 2], [8]) #=> [[4, 1, 8], [5, 2, nil], [6, nil, nil]]
Missing test Not unit tested.
public 92 Array#|
Implementation candidates
|
See also
Usage #uniq
.other_ary
→ ArraySet Union — Returns a new array by joining
ary
with other_ary
, excluding any duplicates and preserving the order from the original array.It compares elements using their
hash
and eql?
methods for efficiency.["a", "b", "c"] | ["c", "d", "a"] #=> ["a", "b", "c", "d"]
Test 98. ruby/test/ruby/test_array.rb:ruby/test/ruby/test_array_andor_0:2/2
524
([1, 2, 3] | [2, 4, 6]) == [1, 2, 3, 4, 6]
Test 99. ruby/test/ruby/test_array.rb:test_0_literal:5/8
525
([1, 2, 3] | [2, 3, 4]) == [1, 2, 3, 4]
public class String
Comparable
┊
String
Kernel
┊
Object
BasicObject
<<String
<<Object
<<BasicObject
Class
Module
Kernel
┊
Object
BasicObject
public 93 String#split
Implementation candidates
split
Test 100. ruby/test/ruby/test_array.rb:test_split_0
Setup code
x = "The Book of Mormon"
y = x.reverse
526
x.split(//).reverse!.join == x.reverse
527
RECEIVER.reverse! == x
528
"1 byte string".split(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1"
Setup code
x = "a b c d"
529
x.split == ["a", "b", "c", "d"]
530
x.split(" ") == ["a", "b", "c", "d"]
Missing Doc No paragraph given for user.