Removes zero or more elements from self
.
When no block is given, removes from self
each element ele
such that ele == obj
; returns the last deleted element:
s1 = 'bar'; s2 = 'bar' a = [:foo, s1, 2, s2] a.delete('bar') # => "bar" a # => [:foo, 2]
Returns nil
if no elements removed.
When a block is given, removes from self
each element ele
such that ele == obj
.
If any such elements are found, ignores the block and returns the last deleted element:
s1 = 'bar'; s2 = 'bar' a = [:foo, s1, 2, s2] deleted_obj = a.delete('bar') {|obj| fail 'Cannot happen' } a # => [:foo, 2]
If no such elements are found, returns the block’s return value:
a = [:foo, 'bar', 2] a.delete(:nosuch) {|obj| "#{obj} not found" } # => "nosuch not found"
Returns a new Array whose elements are all those from self
for which the block returns false
or nil
:
a = [:foo, 'bar', 2, 'bat'] a1 = a.reject {|element| element.to_s.start_with?('b') } a1 # => [:foo, 2]
Returns a new Enumerator
if no block given:
a = [:foo, 'bar', 2] a.reject # => #<Enumerator: [:foo, "bar", 2]:reject>
Removes each element for which the block returns a truthy value.
Returns self
if any elements removed:
a = [:foo, 'bar', 2, 'bat'] a.reject! {|element| element.to_s.start_with?('b') } # => [:foo, 2]
Returns nil
if no elements removed.
Returns a new Enumerator
if no block given:
a = [:foo, 'bar', 2] a.reject! # => #<Enumerator: [:foo, "bar", 2]:reject!>
When no block given, returns a new Array new_array
of size self.size
whose elements are Arrays.
Each nested array new_array[n]
is of size other_arrays.size+1
, and contains:
The nth element of self
.
The nth element of each of the other_arrays
.
If all other_arrays
and self
are the same size:
a = [:a0, :a1, :a2, :a3] b = [:b0, :b1, :b2, :b3] c = [:c0, :c1, :c2, :c3] d = a.zip(b, c) d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
If any array in other_arrays
is smaller than self
, fills to self.size
with nil
:
a = [:a0, :a1, :a2, :a3] b = [:b0, :b1, :b2] c = [:c0, :c1] d = a.zip(b, c) d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, nil], [:a3, nil, nil]]
If any array in other_arrays
is larger than self
, its trailing elements are ignored:
a = [:a0, :a1, :a2, :a3] b = [:b0, :b1, :b2, :b3, :b4] c = [:c0, :c1, :c2, :c3, :c4, :c5] d = a.zip(b, c) d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
When a block is given, calls the block with each of the sub-arrays (formed as above); returns nil
:
a = [:a0, :a1, :a2, :a3] b = [:b0, :b1, :b2, :b3] c = [:c0, :c1, :c2, :c3] a.zip(b, c) {|sub_array| p sub_array} # => nil
Output:
[:a0, :b0, :c0] [:a1, :b1, :c1] [:a2, :b2, :c2] [:a3, :b3, :c3]
Replaces the content of self
with the content of other_array
; returns self
:
a = [:foo, 'bar', 2] a.replace(['foo', :bar, 3]) # => ["foo", :bar, 3]
Replaces specified elements in self
with specified objects; returns self
.
With argument obj
and no block given, replaces all elements with that one object:
a = ['a', 'b', 'c', 'd'] a # => ["a", "b", "c", "d"] a.fill(:X) # => [:X, :X, :X, :X]
With arguments obj
and Integer
start
, and no block given, replaces elements based on the given start.
If start
is in range (0 <= start < array.size
), replaces all elements from offset start
through the end:
a = ['a', 'b', 'c', 'd'] a.fill(:X, 2) # => ["a", "b", :X, :X]
If start
is too large (start >= array.size
), does nothing:
a = ['a', 'b', 'c', 'd'] a.fill(:X, 4) # => ["a", "b", "c", "d"] a = ['a', 'b', 'c', 'd'] a.fill(:X, 5) # => ["a", "b", "c", "d"]
If start
is negative, counts from the end (starting index is start + array.size
):
a = ['a', 'b', 'c', 'd'] a.fill(:X, -2) # => ["a", "b", :X, :X]
If start
is too small (less than and far from zero), replaces all elements:
a = ['a', 'b', 'c', 'd'] a.fill(:X, -6) # => [:X, :X, :X, :X] a = ['a', 'b', 'c', 'd'] a.fill(:X, -50) # => [:X, :X, :X, :X]
With arguments obj
, Integer
start
, and Integer
length
, and no block given, replaces elements based on the given start
and length
.
If start
is in range, replaces length
elements beginning at offset start
:
a = ['a', 'b', 'c', 'd'] a.fill(:X, 1, 1) # => ["a", :X, "c", "d"]
If start
is negative, counts from the end:
a = ['a', 'b', 'c', 'd'] a.fill(:X, -2, 1) # => ["a", "b", :X, "d"]
If start
is large (start >= array.size
), extends self
with nil
:
a = ['a', 'b', 'c', 'd'] a.fill(:X, 5, 0) # => ["a", "b", "c", "d", nil] a = ['a', 'b', 'c', 'd'] a.fill(:X, 5, 2) # => ["a", "b", "c", "d", nil, :X, :X]
If length
is zero or negative, replaces no elements:
a = ['a', 'b', 'c', 'd'] a.fill(:X, 1, 0) # => ["a", "b", "c", "d"] a.fill(:X, 1, -1) # => ["a", "b", "c", "d"]
With arguments obj
and Range
range
, and no block given, replaces elements based on the given range.
If the range is positive and ascending (0 < range.begin <= range.end
), replaces elements from range.begin
to range.end
:
a = ['a', 'b', 'c', 'd'] a.fill(:X, (1..1)) # => ["a", :X, "c", "d"]
If range.first
is negative, replaces no elements:
a = ['a', 'b', 'c', 'd'] a.fill(:X, (-1..1)) # => ["a", "b", "c", "d"]
If range.last
is negative, counts from the end:
a = ['a', 'b', 'c', 'd'] a.fill(:X, (0..-2)) # => [:X, :X, :X, "d"] a = ['a', 'b', 'c', 'd'] a.fill(:X, (1..-2)) # => ["a", :X, :X, "d"]
If range.last
and range.last
are both negative, both count from the end of the array:
a = ['a', 'b', 'c', 'd'] a.fill(:X, (-1..-1)) # => ["a", "b", "c", :X] a = ['a', 'b', 'c', 'd'] a.fill(:X, (-2..-2)) # => ["a", "b", :X, "d"]
With no arguments and a block given, calls the block with each index; replaces the corresponding element with the block’s return value:
a = ['a', 'b', 'c', 'd'] a.fill { |index| "new_#{index}" } # => ["new_0", "new_1", "new_2", "new_3"]
With argument start
and a block given, calls the block with each index from offset start
to the end; replaces the corresponding element with the block’s return value.
If start is in range (0 <= start < array.size
), replaces from offset start
to the end:
a = ['a', 'b', 'c', 'd'] a.fill(1) { |index| "new_#{index}" } # => ["a", "new_1", "new_2", "new_3"]
If start
is too large(start >= array.size
), does nothing:
a = ['a', 'b', 'c', 'd'] a.fill(4) { |index| fail 'Cannot happen' } # => ["a", "b", "c", "d"] a = ['a', 'b', 'c', 'd'] a.fill(4) { |index| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
If start
is negative, counts from the end:
a = ['a', 'b', 'c', 'd'] a.fill(-2) { |index| "new_#{index}" } # => ["a", "b", "new_2", "new_3"]
If start is too small (start <= -array.size
, replaces all elements:
a = ['a', 'b', 'c', 'd'] a.fill(-6) { |index| "new_#{index}" } # => ["new_0", "new_1", "new_2", "new_3"] a = ['a', 'b', 'c', 'd'] a.fill(-50) { |index| "new_#{index}" } # => ["new_0", "new_1", "new_2", "new_3"]
With arguments start
and length
, and a block given, calls the block for each index specified by start length; replaces the corresponding element with the block’s return value.
If start
is in range, replaces length
elements beginning at offset start
:
a = ['a', 'b', 'c', 'd'] a.fill(1, 1) { |index| "new_#{index}" } # => ["a", "new_1", "c", "d"]
If start is negative, counts from the end:
a = ['a', 'b', 'c', 'd'] a.fill(-2, 1) { |index| "new_#{index}" } # => ["a", "b", "new_2", "d"]
If start
is large (start >= array.size
), extends self
with nil
:
a = ['a', 'b', 'c', 'd'] a.fill(5, 0) { |index| "new_#{index}" } # => ["a", "b", "c", "d", nil] a = ['a', 'b', 'c', 'd'] a.fill(5, 2) { |index| "new_#{index}" } # => ["a", "b", "c", "d", nil, "new_5", "new_6"]
If length
is zero or less, replaces no elements:
a = ['a', 'b', 'c', 'd'] a.fill(1, 0) { |index| "new_#{index}" } # => ["a", "b", "c", "d"] a.fill(1, -1) { |index| "new_#{index}" } # => ["a", "b", "c", "d"]
With arguments obj
and range
, and a block given, calls the block with each index in the given range; replaces the corresponding element with the block’s return value.
If the range is positive and ascending (range 0 < range.begin <= range.end
, replaces elements from range.begin
to range.end
:
a = ['a', 'b', 'c', 'd'] a.fill(1..1) { |index| "new_#{index}" } # => ["a", "new_1", "c", "d"]
If range.first
is negative, does nothing:
a = ['a', 'b', 'c', 'd'] a.fill(-1..1) { |index| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
If range.last
is negative, counts from the end:
a = ['a', 'b', 'c', 'd'] a.fill(0..-2) { |index| "new_#{index}" } # => ["new_0", "new_1", "new_2", "d"] a = ['a', 'b', 'c', 'd'] a.fill(1..-2) { |index| "new_#{index}" } # => ["a", "new_1", "new_2", "d"]
If range.first
and range.last
are both negative, both count from the end:
a = ['a', 'b', 'c', 'd'] a.fill(-1..-1) { |index| "new_#{index}" } # => ["a", "b", "c", "new_3"] a = ['a', 'b', 'c', 'd'] a.fill(-2..-2) { |index| "new_#{index}" } # => ["a", "b", "new_2", "d"]
Returns true
if for some index i
in self
, obj == self[i]
; otherwise false
:
[0, 1, 2].include?(2) # => true [0, 1, 2].include?(3) # => false
Returns -1, 0, or 1 as self
is less than, equal to, or greater than other_array
. For each index i
in self
, evaluates result = self[i] <=> other_array[i]
.
Returns -1 if any result is -1:
[0, 1, 2] <=> [0, 1, 3] # => -1
Returns 1 if any result is 1:
[0, 1, 2] <=> [0, 1, 1] # => 1
When all results are zero:
Returns -1 if array
is smaller than other_array
:
[0, 1, 2] <=> [0, 1, 2, 3] # => -1
Returns 1 if array
is larger than other_array
:
[0, 1, 2] <=> [0, 1] # => 1
Returns 0 if array
and other_array
are the same size:
[0, 1, 2] <=> [0, 1, 2] # => 0
Returns elements from self
; does not modify self
.
When a single Integer
argument index
is given, returns the element at offset index
:
a = [:foo, 'bar', 2] a[0] # => :foo a[2] # => 2 a # => [:foo, "bar", 2]
If index
is negative, counts relative to the end of self
:
a = [:foo, 'bar', 2] a[-1] # => 2 a[-2] # => "bar"
If index
is out of range, returns nil
.
When two Integer
arguments start
and length
are given, returns a new Array of size length
containing successive elements beginning at offset start
:
a = [:foo, 'bar', 2] a[0, 2] # => [:foo, "bar"] a[1, 2] # => ["bar", 2]
If start + length
is greater than self.length
, returns all elements from offset start
to the end:
a = [:foo, 'bar', 2] a[0, 4] # => [:foo, "bar", 2] a[1, 3] # => ["bar", 2] a[2, 2] # => [2]
If start == self.size
and length >= 0
, returns a new empty Array.
If length
is negative, returns nil
.
When a single Range
argument range
is given, treats range.min
as start
above and range.size
as length
above:
a = [:foo, 'bar', 2] a[0..1] # => [:foo, "bar"] a[1..2] # => ["bar", 2]
Special case: If range.start == a.size
, returns a new empty Array.
If range.end
is negative, calculates the end index from the end:
a = [:foo, 'bar', 2] a[0..-1] # => [:foo, "bar", 2] a[0..-2] # => [:foo, "bar"] a[0..-3] # => [:foo]
If range.start
is negative, calculates the start index from the end:
a = [:foo, 'bar', 2] a[-1..2] # => [2] a[-2..2] # => ["bar", 2] a[-3..2] # => [:foo, "bar", 2]
If range.start
is larger than the array size, returns nil
.
a = [:foo, 'bar', 2] a[4..1] # => nil a[4..0] # => nil a[4..-1] # => nil
When a single Enumerator::ArithmeticSequence
argument aseq
is given, returns an Array of elements corresponding to the indexes produced by the sequence.
a = ['--', 'data1', '--', 'data2', '--', 'data3'] a[(1..).step(2)] # => ["data1", "data2", "data3"]
Unlike slicing with range, if the start or the end of the arithmetic sequence is larger than array size, throws RangeError
.
a = ['--', 'data1', '--', 'data2', '--', 'data3'] a[(1..11).step(2)] # RangeError (((1..11).step(2)) out of range) a[(7..).step(2)] # RangeError (((7..).step(2)) out of range)
If given a single argument, and its type is not one of the listed, tries to convert it to Integer
, and raises if it is impossible:
a = [:foo, 'bar', 2] # Raises TypeError (no implicit conversion of Symbol into Integer): a[:foo]
Removes and returns elements from self
.
When the only argument is an Integer
n
, removes and returns the nth element in self
:
a = [:foo, 'bar', 2] a.slice!(1) # => "bar" a # => [:foo, 2]
If n
is negative, counts backwards from the end of self
:
a = [:foo, 'bar', 2] a.slice!(-1) # => 2 a # => [:foo, "bar"]
If n
is out of range, returns nil
.
When the only arguments are Integers start
and length
, removes length
elements from self
beginning at offset start
; returns the deleted objects in a new Array:
a = [:foo, 'bar', 2] a.slice!(0, 2) # => [:foo, "bar"] a # => [2]
If start + length
exceeds the array size, removes and returns all elements from offset start
to the end:
a = [:foo, 'bar', 2] a.slice!(1, 50) # => ["bar", 2] a # => [:foo]
If start == a.size
and length
is non-negative, returns a new empty Array.
If length
is negative, returns nil
.
When the only argument is a Range
object range
, treats range.min
as start
above and range.size
as length
above:
a = [:foo, 'bar', 2] a.slice!(1..2) # => ["bar", 2] a # => [:foo]
If range.start == a.size
, returns a new empty Array.
If range.start
is larger than the array size, returns nil
.
If range.end
is negative, counts backwards from the end of the array:
a = [:foo, 'bar', 2] a.slice!(0..-2) # => [:foo, "bar"] a # => [2]
If range.start
is negative, calculates the start index backwards from the end of the array:
a = [:foo, 'bar', 2] a.slice!(-2..2) # => ["bar", 2] a # => [:foo]
Returns the first element in self
that is an Array whose first element ==
obj
:
a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]] a.assoc(4) # => [4, 5, 6]
Returns nil
if no such element is found.
Related: rassoc
.
Returns a new Array containing all elements of array
followed by all elements of other_array
:
a = [0, 1] + [2, 3] a # => [0, 1, 2, 3]
Related: concat
.
When non-negative argument Integer
n
is given, returns a new Array built by concatenating the n
copies of self
:
a = ['x', 'y'] a * 3 # => ["x", "y", "x", "y", "x", "y"]
When String
argument string_separator
is given, equivalent to array.join(string_separator)
:
[0, [0, 1], {foo: 0}] * ', ' # => "0, 0, 1, {:foo=>0}"
Returns a new Array containing only those elements from array
that are not found in Array other_array
; items are compared using eql?
; the order from array
is preserved:
[0, 1, 1, 2, 1, 1, 3, 1, 1] - [1] # => [0, 2, 3] [0, 1, 2, 3] - [3, 0] # => [1, 2] [0, 1, 2] - [4] # => [0, 1, 2]
Related: Array#difference
.
Returns a new Array containing each element found in both array
and Array other_array
; duplicates are omitted; items are compared using eql?
(items must also implement hash
correctly):
[0, 1, 2, 3] & [1, 2] # => [1, 2] [0, 1, 0, 1] & [0, 1] # => [0, 1]
Preserves order from array
:
[0, 1, 2] & [3, 2, 1, 0] # => [0, 1, 2]
Related: Array#intersection
.
Returns the union of array
and Array other_array
; duplicates are removed; order is preserved; items are compared using eql?
:
[0, 1] | [2, 3] # => [0, 1, 2, 3] [0, 1, 1] | [2, 2, 3] # => [0, 1, 2, 3] [0, 1, 2] | [3, 2, 1, 0] # => [0, 1, 2, 3]
Related: Array#union
.
Returns one of the following:
The maximum-valued element from self
.
A new Array of maximum-valued elements selected from self
.
When no block is given, each element in self
must respond to method <=>
with an Integer
.
With no argument and no block, returns the element in self
having the maximum value per method <=>
:
[0, 1, 2].max # => 2
With an argument Integer
n
and no block, returns a new Array with at most n
elements, in descending order per method <=>
:
[0, 1, 2, 3].max(3) # => [3, 2, 1] [0, 1, 2, 3].max(6) # => [3, 2, 1, 0]
When a block is given, the block must return an Integer
.
With a block and no argument, calls the block self.size-1
times to compare elements; returns the element having the maximum value per the block:
['0', '00', '000'].max {|a, b| a.size <=> b.size } # => "000"
With an argument n
and a block, returns a new Array with at most n
elements, in descending order per the block:
['0', '00', '000'].max(2) {|a, b| a.size <=> b.size } # => ["000", "00"]
Returns one of the following:
The minimum-valued element from self
.
A new Array of minimum-valued elements selected from self
.
When no block is given, each element in self
must respond to method <=>
with an Integer
.
With no argument and no block, returns the element in self
having the minimum value per method <=>
:
[0, 1, 2].min # => 0
With Integer
argument n
and no block, returns a new Array with at most n
elements, in ascending order per method <=>
:
[0, 1, 2, 3].min(3) # => [0, 1, 2] [0, 1, 2, 3].min(6) # => [0, 1, 2, 3]
When a block is given, the block must return an Integer
.
With a block and no argument, calls the block self.size-1
times to compare elements; returns the element having the minimum value per the block:
['0', '00', '000'].min { |a, b| a.size <=> b.size } # => "0"
With an argument n
and a block, returns a new Array with at most n
elements, in ascending order per the block:
['0', '00', '000'].min(2) {|a, b| a.size <=> b.size } # => ["0", "00"]
Returns a new 2-element Array containing the minimum and maximum values from self
, either per method <=>
or per a given block:.
When no block is given, each element in self
must respond to method <=>
with an Integer
; returns a new 2-element Array containing the minimum and maximum values from self
, per method <=>
:
[0, 1, 2].minmax # => [0, 2]
When a block is given, the block must return an Integer
; the block is called self.size-1
times to compare elements; returns a new 2-element Array containing the minimum and maximum values from self
, per the block:
['0', '00', '000'].minmax {|a, b| a.size <=> b.size } # => ["0", "000"]
Returns a new Array containing those elements from self
that are not duplicates, the first occurrence always being retained.
With no block given, identifies and omits duplicates using method eql?
to compare:
a = [0, 0, 1, 1, 2, 2] a.uniq # => [0, 1, 2]
With a block given, calls the block for each element; identifies (using method eql?
) and omits duplicate values, that is, those elements for which the block returns the same value:
a = ['a', 'aa', 'aaa', 'b', 'bb', 'bbb'] a.uniq {|element| element.size } # => ["a", "aa", "aaa"]
Removes duplicate elements from self
, the first occurrence always being retained; returns self
if any elements removed, nil
otherwise.
With no block given, identifies and removes elements using method eql?
to compare.
Returns self
if any elements removed:
a = [0, 0, 1, 1, 2, 2] a.uniq! # => [0, 1, 2]
Returns nil
if no elements removed.
With a block given, calls the block for each element; identifies (using method eql?
) and removes elements for which the block returns duplicate values.
Returns self
if any elements removed:
a = ['a', 'aa', 'aaa', 'b', 'bb', 'bbb'] a.uniq! {|element| element.size } # => ['a', 'aa', 'aaa']
Returns nil
if no elements removed.
Returns a new Array containing all non-nil
elements from self
:
a = [nil, 0, nil, 1, nil, 2, nil] a.compact # => [0, 1, 2]
Removes all nil
elements from self
.
Returns self
if any elements removed, otherwise nil
.
Returns a new Array that is a recursive flattening of self
:
Each non-Array element is unchanged.
Each Array is replaced by its individual elements.
With non-negative Integer
argument level
, flattens recursively through level
levels:
a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten(0) # => [0, [1, [2, 3], 4], 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten(1) # => [0, 1, [2, 3], 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten(2) # => [0, 1, 2, 3, 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten(3) # => [0, 1, 2, 3, 4, 5]
With no argument, a nil
argument, or with negative argument level
, flattens all levels:
a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten # => [0, 1, 2, 3, 4, 5] [0, 1, 2].flatten # => [0, 1, 2] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten(-1) # => [0, 1, 2, 3, 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten(-2) # => [0, 1, 2, 3, 4, 5] [0, 1, 2].flatten(-1) # => [0, 1, 2]
Replaces each nested Array in self
with the elements from that Array; returns self
if any changes, nil
otherwise.
With non-negative Integer
argument level
, flattens recursively through level
levels:
a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten!(1) # => [0, 1, [2, 3], 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten!(2) # => [0, 1, 2, 3, 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten!(3) # => [0, 1, 2, 3, 4, 5] [0, 1, 2].flatten!(1) # => nil
With no argument, a nil
argument, or with negative argument level
, flattens all levels:
a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten! # => [0, 1, 2, 3, 4, 5] [0, 1, 2].flatten! # => nil a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten!(-1) # => [0, 1, 2, 3, 4, 5] a = [ 0, [ 1, [2, 3], 4 ], 5 ] a.flatten!(-2) # => [0, 1, 2, 3, 4, 5] [0, 1, 2].flatten!(-1) # => nil