Removes zero or more elements from self
.
With no block given, removes from self
each element ele
such that ele == object
; returns the last removed element:
a = [0, 1, 2, 2.0] a.delete(2) # => 2.0 a # => [0, 1]
Returns nil
if no elements removed:
a.delete(2) # => nil
With a block given, removes from self
each element ele
such that ele == object
.
If any such elements are found, ignores the block and returns the last removed element:
a = [0, 1, 2, 2.0] a.delete(2) {|element| fail 'Cannot happen' } # => 2.0 a # => [0, 1]
If no such element is found, returns the block’s return value:
a.delete(2) {|element| "Element #{element} not found." } # => "Element 2 not found."
Related: see Methods for Deleting.
With a block given, 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]
With no block given, returns a new Enumerator
.
Related: Methods for Fetching.
With a block given, calls the block with each element of self
; 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.
With no block given, returns a new Enumerator
.
Related: see Methods for Deleting.
With no block given, combines self
with the collection of other_arrays
; returns a new array of sub-arrays:
[0, 1].zip(['zero', 'one'], [:zero, :one]) # => [[0, "zero", :zero], [1, "one", :one]]
Returned:
The outer array is of size self.size
.
Each sub-array is of size other_arrays.size + 1
.
The nth sub-array contains (in order):
The nth element of self
.
The nth element of each of the other arrays, as available.
Example:
a = [0, 1] zipped = a.zip(['zero', 'one'], [:zero, :one]) # => [[0, "zero", :zero], [1, "one", :one]] zipped.size # => 2 # Same size as a. zipped.first.size # => 3 # Size of other arrays plus 1.
When the other arrays are all the same size as self
, the returned sub-arrays are a rearrangement containing exactly elements of all the arrays (including self
), with no omissions or additions:
a = [:a0, :a1, :a2, :a3] b = [:b0, :b1, :b2, :b3] c = [:c0, :c1, :c2, :c3] d = a.zip(b, c) pp d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
When one of the other arrays is smaller than self
, pads the corresponding sub-array with nil
elements:
a = [:a0, :a1, :a2, :a3] b = [:b0, :b1, :b2] c = [:c0, :c1] d = a.zip(b, c) pp d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, nil], [:a3, nil, nil]]
When one of the other arrays is larger than self
, ignores its trailing elements:
a = [:a0, :a1, :a2, :a3] b = [:b0, :b1, :b2, :b3, :b4] c = [:c0, :c1, :c2, :c3, :c4, :c5] d = a.zip(b, c) pp d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
With a block given, calls the block with each of the other arrays; returns nil
:
d = [] a = [:a0, :a1, :a2, :a3] b = [:b0, :b1, :b2, :b3] c = [:c0, :c1, :c2, :c3] a.zip(b, c) {|sub_array| d.push(sub_array.reverse) } # => nil pp d # => [[:c0, :b0, :a0], [:c1, :b1, :a1], [:c2, :b2, :a2], [:c3, :b3, :a3]]
For an object in other_arrays that is not actually an array, forms the the “other array” as object.to_ary
, if defined, or as object.each.to_a
otherwise.
Related: see Methods for Converting.
Replaces the elements of self
with the elements of other_array
, which must be an array-convertible object; returns self
:
a = ['a', 'b', 'c'] # => ["a", "b", "c"] a.replace(['d', 'e']) # => ["d", "e"]
Related: see Methods for Assigning.
Replaces selected elements in self
; may add elements to self
; always returns self
(never a new array).
In brief:
# Non-negative start. ['a', 'b', 'c', 'd'].fill('-', 1, 2) # => ["a", "-", "-", "d"] ['a', 'b', 'c', 'd'].fill(1, 2) {|e| e.to_s } # => ["a", "1", "2", "d"] # Extends with specified values if necessary. ['a', 'b', 'c', 'd'].fill('-', 3, 2) # => ["a", "b", "c", "-", "-"] ['a', 'b', 'c', 'd'].fill(3, 2) {|e| e.to_s } # => ["a", "b", "c", "3", "4"] # Fills with nils if necessary. ['a', 'b', 'c', 'd'].fill('-', 6, 2) # => ["a", "b", "c", "d", nil, nil, "-", "-"] ['a', 'b', 'c', 'd'].fill(6, 2) {|e| e.to_s } # => ["a", "b", "c", "d", nil, nil, "6", "7"] # For negative start, counts backwards from the end. ['a', 'b', 'c', 'd'].fill('-', -3, 3) # => ["a", "-", "-", "-"] ['a', 'b', 'c', 'd'].fill(-3, 3) {|e| e.to_s } # => ["a", "1", "2", "3"] # Range. ['a', 'b', 'c', 'd'].fill('-', 1..2) # => ["a", "-", "-", "d"] ['a', 'b', 'c', 'd'].fill(1..2) {|e| e.to_s } # => ["a", "1", "2", "d"]
When arguments start
and count
are given, they select the elements of self
to be replaced; each must be an integer-convertible object (or nil
):
start
specifies the zero-based offset of the first element to be replaced; nil
means zero.
count
is the number of consecutive elements to be replaced; nil
means “all the rest.”
With argument object
given, that one object is used for all replacements:
o = Object.new # => #<Object:0x0000014e7bff7600> a = ['a', 'b', 'c', 'd'] # => ["a", "b", "c", "d"] a.fill(o, 1, 2) # => ["a", #<Object:0x0000014e7bff7600>, #<Object:0x0000014e7bff7600>, "d"]
With a block given, the block is called once for each element to be replaced; the value passed to the block is the index of the element to be replaced (not the element itself); the block’s return value replaces the element:
a = ['a', 'b', 'c', 'd'] # => ["a", "b", "c", "d"] a.fill(1, 2) {|element| element.to_s } # => ["a", "1", "2", "d"]
For arguments start
and count
:
If start
is non-negative, replaces count
elements beginning at offset start
:
['a', 'b', 'c', 'd'].fill('-', 0, 2) # => ["-", "-", "c", "d"] ['a', 'b', 'c', 'd'].fill('-', 1, 2) # => ["a", "-", "-", "d"] ['a', 'b', 'c', 'd'].fill('-', 2, 2) # => ["a", "b", "-", "-"] ['a', 'b', 'c', 'd'].fill(0, 2) {|e| e.to_s } # => ["0", "1", "c", "d"] ['a', 'b', 'c', 'd'].fill(1, 2) {|e| e.to_s } # => ["a", "1", "2", "d"] ['a', 'b', 'c', 'd'].fill(2, 2) {|e| e.to_s } # => ["a", "b", "2", "3"]
Extends self
if necessary:
['a', 'b', 'c', 'd'].fill('-', 3, 2) # => ["a", "b", "c", "-", "-"] ['a', 'b', 'c', 'd'].fill('-', 4, 2) # => ["a", "b", "c", "d", "-", "-"] ['a', 'b', 'c', 'd'].fill(3, 2) {|e| e.to_s } # => ["a", "b", "c", "3", "4"] ['a', 'b', 'c', 'd'].fill(4, 2) {|e| e.to_s } # => ["a", "b", "c", "d", "4", "5"]
Fills with nil
if necessary:
['a', 'b', 'c', 'd'].fill('-', 5, 2) # => ["a", "b", "c", "d", nil, "-", "-"] ['a', 'b', 'c', 'd'].fill('-', 6, 2) # => ["a", "b", "c", "d", nil, nil, "-", "-"] ['a', 'b', 'c', 'd'].fill(5, 2) {|e| e.to_s } # => ["a", "b", "c", "d", nil, "5", "6"] ['a', 'b', 'c', 'd'].fill(6, 2) {|e| e.to_s } # => ["a", "b", "c", "d", nil, nil, "6", "7"]
Does nothing if count
is non-positive:
['a', 'b', 'c', 'd'].fill('-', 2, 0) # => ["a", "b", "c", "d"] ['a', 'b', 'c', 'd'].fill('-', 2, -100) # => ["a", "b", "c", "d"] ['a', 'b', 'c', 'd'].fill('-', 6, -100) # => ["a", "b", "c", "d"] ['a', 'b', 'c', 'd'].fill(2, 0) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"] ['a', 'b', 'c', 'd'].fill(2, -100) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"] ['a', 'b', 'c', 'd'].fill(6, -100) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
If start
is negative, counts backwards from the end of self
:
['a', 'b', 'c', 'd'].fill('-', -4, 3) # => ["-", "-", "-", "d"] ['a', 'b', 'c', 'd'].fill('-', -3, 3) # => ["a", "-", "-", "-"] ['a', 'b', 'c', 'd'].fill(-4, 3) {|e| e.to_s } # => ["0", "1", "2", "d"] ['a', 'b', 'c', 'd'].fill(-3, 3) {|e| e.to_s } # => ["a", "1", "2", "3"]
Extends self
if necessary:
['a', 'b', 'c', 'd'].fill('-', -2, 3) # => ["a", "b", "-", "-", "-"] ['a', 'b', 'c', 'd'].fill('-', -1, 3) # => ["a", "b", "c", "-", "-", "-"] ['a', 'b', 'c', 'd'].fill(-2, 3) {|e| e.to_s } # => ["a", "b", "2", "3", "4"] ['a', 'b', 'c', 'd'].fill(-1, 3) {|e| e.to_s } # => ["a", "b", "c", "3", "4", "5"]
Starts at the beginning of self
if start
is negative and out-of-range:
['a', 'b', 'c', 'd'].fill('-', -5, 2) # => ["-", "-", "c", "d"] ['a', 'b', 'c', 'd'].fill('-', -6, 2) # => ["-", "-", "c", "d"] ['a', 'b', 'c', 'd'].fill(-5, 2) {|e| e.to_s } # => ["0", "1", "c", "d"] ['a', 'b', 'c', 'd'].fill(-6, 2) {|e| e.to_s } # => ["0", "1", "c", "d"]
Does nothing if count
is non-positive:
['a', 'b', 'c', 'd'].fill('-', -2, 0) # => ["a", "b", "c", "d"] ['a', 'b', 'c', 'd'].fill('-', -2, -1) # => ["a", "b", "c", "d"] ['a', 'b', 'c', 'd'].fill(-2, 0) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"] ['a', 'b', 'c', 'd'].fill(-2, -1) {|e| fail 'Cannot happen' } # => ["a", "b", "c", "d"]
When argument range
is given, it must be a Range
object whose members are numeric; its begin
and end
values determine the elements of self
to be replaced:
If both begin
and end
are positive, they specify the first and last elements to be replaced:
['a', 'b', 'c', 'd'].fill('-', 1..2) # => ["a", "-", "-", "d"] ['a', 'b', 'c', 'd'].fill(1..2) {|e| e.to_s } # => ["a", "1", "2", "d"]
If end
is smaller than begin
, replaces no elements:
['a', 'b', 'c', 'd'].fill('-', 2..1) # => ["a", "b", "c", "d"] ['a', 'b', 'c', 'd'].fill(2..1) {|e| e.to_s } # => ["a", "b", "c", "d"]
If either is negative (or both are negative), counts backwards from the end of self
:
['a', 'b', 'c', 'd'].fill('-', -3..2) # => ["a", "-", "-", "d"] ['a', 'b', 'c', 'd'].fill('-', 1..-2) # => ["a", "-", "-", "d"] ['a', 'b', 'c', 'd'].fill('-', -3..-2) # => ["a", "-", "-", "d"] ['a', 'b', 'c', 'd'].fill(-3..2) {|e| e.to_s } # => ["a", "1", "2", "d"] ['a', 'b', 'c', 'd'].fill(1..-2) {|e| e.to_s } # => ["a", "1", "2", "d"] ['a', 'b', 'c', 'd'].fill(-3..-2) {|e| e.to_s } # => ["a", "1", "2", "d"]
If the end
value is excluded (see Range#exclude_end?
), omits the last replacement:
['a', 'b', 'c', 'd'].fill('-', 1...2) # => ["a", "-", "c", "d"] ['a', 'b', 'c', 'd'].fill('-', 1...-2) # => ["a", "-", "c", "d"] ['a', 'b', 'c', 'd'].fill(1...2) {|e| e.to_s } # => ["a", "1", "c", "d"] ['a', 'b', 'c', 'd'].fill(1...-2) {|e| e.to_s } # => ["a", "1", "c", "d"]
If the range is endless (see Endless Ranges), replaces elements to the end of self
:
['a', 'b', 'c', 'd'].fill('-', 1..) # => ["a", "-", "-", "-"] ['a', 'b', 'c', 'd'].fill(1..) {|e| e.to_s } # => ["a", "1", "2", "3"]
If the range is beginless (see Beginless Ranges), replaces elements from the beginning of self
:
['a', 'b', 'c', 'd'].fill('-', ..2) # => ["-", "-", "-", "d"] ['a', 'b', 'c', 'd'].fill(..2) {|e| e.to_s } # => ["0", "1", "2", "d"]
Related: see Methods for Assigning.
Returns whether for some element element
in self
, object == element
:
[0, 1, 2].include?(2) # => true [0, 1, 2].include?(2.0) # => true [0, 1, 2].include?(2.1) # => false
Related: see Methods for Querying.
Returns -1, 0, or 1 as self
is determined to be less than, equal to, or greater than other_array
.
Iterates over each index i
in (0...self.size)
:
Computes result[i]
as self[i] <=> other_array[i]
.
Immediately returns 1 if result[i]
is 1:
[0, 1, 2] <=> [0, 0, 2] # => 1
Immediately returns -1 if result[i]
is -1:
[0, 1, 2] <=> [0, 2, 2] # => -1
Continues if result[i]
is 0.
When every result
is 0, returns self.size <=> other_array.size
(see Integer#<=>
):
[0, 1, 2] <=> [0, 1] # => 1 [0, 1, 2] <=> [0, 1, 2] # => 0 [0, 1, 2] <=> [0, 1, 2, 3] # => -1
Note that when other_array
is larger than self
, its trailing elements do not affect the result:
[0, 1, 2] <=> [0, 1, 2, -3] # => -1 [0, 1, 2] <=> [0, 1, 2, 0] # => -1 [0, 1, 2] <=> [0, 1, 2, 3] # => -1
Related: see Methods for Comparing.
Returns elements from self
; does not modify self
.
In brief:
a = [:foo, 'bar', 2] # Single argument index: returns one element. a[0] # => :foo # Zero-based index. a[-1] # => 2 # Negative index counts backwards from end. # Arguments start and length: returns an array. a[1, 2] # => ["bar", 2] a[-2, 2] # => ["bar", 2] # Negative start counts backwards from end. # Single argument range: returns an array. a[0..1] # => [:foo, "bar"] a[0..-2] # => [:foo, "bar"] # Negative range-begin counts backwards from end. a[-2..2] # => ["bar", 2] # Negative range-end counts backwards from end.
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 backwards from 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]
Related: see Methods for Fetching.
Removes and returns elements from self
.
With numeric argument index
given, removes and returns the element at offset index
:
a = ['a', 'b', 'c', 'd'] a.slice!(2) # => "c" a # => ["a", "b", "d"] a.slice!(2.1) # => "d" a # => ["a", "b"]
If index
is negative, counts backwards from the end of self
:
a = ['a', 'b', 'c', 'd'] a.slice!(-2) # => "c" a # => ["a", "b", "d"]
If index
is out of range, returns nil
.
With numeric arguments start
and length
given, removes length
elements from self
beginning at zero-based offset start
; returns the removed objects in a new array:
a = ['a', 'b', 'c', 'd'] a.slice!(1, 2) # => ["b", "c"] a # => ["a", "d"] a.slice!(0.1, 1.1) # => ["a"] a # => ["d"]
If start
is negative, counts backwards from the end of self
:
a = ['a', 'b', 'c', 'd'] a.slice!(-2, 1) # => ["c"] a # => ["a", "b", "d"]
If start
is out-of-range, returns nil
:
a = ['a', 'b', 'c', 'd'] a.slice!(5, 1) # => nil a.slice!(-5, 1) # => nil
If start + length
exceeds the array size, removes and returns all elements from offset start
to the end:
a = ['a', 'b', 'c', 'd'] a.slice!(2, 50) # => ["c", "d"] a # => ["a", "b"]
If start == a.size
and length
is non-negative, returns a new empty array.
If length
is negative, returns nil
.
With Range
argument range
given, treats range.min
as start
(as above) and range.size
as length
(as above):
a = ['a', 'b', 'c', 'd'] a.slice!(1..2) # => ["b", "c"] a # => ["a", "d"]
If range.start == a.size
, returns a new empty array:
a = ['a', 'b', 'c', 'd'] a.slice!(4..5) # => []
If range.start
is larger than the array size, returns nil
:
a = ['a', 'b', 'c', 'd'] a.slice!(5..6) # => nil
If range.start
is negative, calculates the start index by counting backwards from the end of self
:
a = ['a', 'b', 'c', 'd'] a.slice!(-2..2) # => ["c"]
If range.end
is negative, calculates the end index by counting backwards from the end of self
:
a = ['a', 'b', 'c', 'd'] a.slice!(0..-2) # => ["a", "b", "c"]
Related: see Methods for Deleting.
Returns the first element ele
in self
such that ele
is an array and ele[0] == object
:
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: Array#rassoc
; see also Methods for Fetching.
Returns a new array containing all elements of self
followed by all elements of other_array
:
a = [0, 1] + [2, 3] a # => [0, 1, 2, 3]
Related: see Methods for Combining.
When non-negative integer argument n
is given, returns a new array built by concatenating n
copies of self
:
a = ['x', 'y'] a * 3 # => ["x", "y", "x", "y", "x", "y"]
When string argument string_separator
is given, equivalent to self.join(string_separator)
:
[0, [0, 1], {foo: 0}] * ', ' # => "0, 0, 1, {foo: 0}"
Returns a new array containing only those elements of self
that are not found in other_array
; the order from self
is preserved:
[0, 1, 1, 2, 1, 1, 3, 1, 1] - [1] # => [0, 2, 3] [0, 1, 1, 2, 1, 1, 3, 1, 1] - [3, 2, 0, :foo] # => [1, 1, 1, 1, 1, 1] [0, 1, 2] - [:foo] # => [0, 1, 2]
Element are compared using method eql?
(as defined in each element of self
).
Related: see Methods for Combining.
Returns a new array containing the intersection of self
and other_array
; that is, containing those elements found in both self
and other_array
:
[0, 1, 2, 3] & [1, 2] # => [1, 2]
Omits duplicates:
[0, 1, 1, 0] & [0, 1] # => [0, 1]
Preserves order from self
:
[0, 1, 2] & [3, 2, 1, 0] # => [0, 1, 2]
Identifies common elements using method eql?
(as defined in each element of self
).
Related: see Methods for Combining.
Returns the union of self
and 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: see Methods for Combining.
Returns one of the following:
The maximum-valued element from self
.
A new array of maximum-valued elements from self
.
Does not modify self
.
With no block given, each element in self
must respond to method <=>
with a numeric.
With no argument and no block, returns the element in self
having the maximum value per method <=>
:
[1, 0, 3, 2].max # => 3
With non-negative numeric argument count
and no block, returns a new array with at most count
elements, in descending order, per method <=>
:
[1, 0, 3, 2].max(3) # => [3, 2, 1] [1, 0, 3, 2].max(3.0) # => [3, 2, 1] [1, 0, 3, 2].max(9) # => [3, 2, 1, 0] [1, 0, 3, 2].max(0) # => []
With a block given, the block must return a numeric.
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', '', '000', '00'].max {|a, b| a.size <=> b.size } # => "000"
With non-negative numeric argument count
and a block, returns a new array with at most count
elements, in descending order, per the block:
['0', '', '000', '00'].max(2) {|a, b| a.size <=> b.size } # => ["000", "00"]
Related: see Methods for Fetching.
Returns one of the following:
The minimum-valued element from self
.
A new array of minimum-valued elements from self
.
Does not modify self
.
With no block given, each element in self
must respond to method <=>
with a numeric.
With no argument and no block, returns the element in self
having the minimum value per method <=>
:
[1, 0, 3, 2].min # => 0
With non-negative numeric argument count
and no block, returns a new array with at most count
elements, in ascending order, per method <=>
:
[1, 0, 3, 2].min(3) # => [0, 1, 2] [1, 0, 3, 2].min(3.0) # => [0, 1, 2] [1, 0, 3, 2].min(9) # => [0, 1, 2, 3] [1, 0, 3, 2].min(0) # => []
With a block given, the block must return a numeric.
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', '', '000', '00'].min {|a, b| a.size <=> b.size } # => ""
With non-negative numeric argument count
and a block, returns a new array with at most count
elements, in ascending order, per the block:
['0', '', '000', '00'].min(2) {|a, b| a.size <=> b.size } # => ["", "0"]
Related: see Methods for Fetching.
Returns a 2-element array containing the minimum-valued and maximum-valued elements from self
; does not modify self
.
With no block given, the minimum and maximum values are determined using method <=>
:
[1, 0, 3, 2].minmax # => [0, 3]
With a block given, the block must return a numeric; the block is called self.size - 1
times to compare elements; returns the elements having the minimum and maximum values per the block:
['0', '', '000', '00'].minmax {|a, b| a.size <=> b.size } # => ["", "000"]
Related: see Methods for Fetching.
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 duplicate elements using method eql?
to compare elements:
a = [0, 0, 1, 1, 2, 2] a.uniq # => [0, 1, 2]
With a block given, calls the block for each element; identifies and omits “duplicate” elements using method eql?
to compare block return values; that is, an element is a duplicate if its block return value is the same as that of a previous element:
a = ['a', 'aa', 'aaa', 'b', 'bb', 'bbb'] a.uniq {|element| element.size } # => ["a", "aa", "aaa"]
Related: Methods for Fetching.
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 elements:
a = [0, 0, 1, 1, 2, 2] a.uniq! # => [0, 1, 2] a.uniq! # => nil
With a block given, calls the block for each element; identifies and omits “duplicate” elements using method eql?
to compare block return values; that is, an element is a duplicate if its block return value is the same as that of a previous element:
a = ['a', 'aa', 'aaa', 'b', 'bb', 'bbb'] a.uniq! {|element| element.size } # => ["a", "aa", "aaa"] a.uniq! {|element| element.size } # => nil
Related: see Methods for Deleting.
Returns a new array containing only the non-nil
elements from self
; element order is preserved:
a = [nil, 0, nil, false, nil, '', nil, [], nil, {}] a.compact # => [0, false, "", [], {}]
Related: Array#compact!
; see also Methods for Deleting.
Removes all nil
elements from self
; Returns self
if any elements are removed, nil
otherwise:
a = [nil, 0, nil, false, nil, '', nil, [], nil, {}] a.compact! # => [0, false, "", [], {}] a # => [0, false, "", [], {}] a.compact! # => nil
Related: Array#compact
; see also Methods for Deleting.
Returns a new array that is a recursive flattening of self
to depth
levels of recursion; depth
must be an integer-convertible object or nil
. At each level of recursion:
Each element that is an array is “flattened” (that is, replaced by its individual array elements).
Each element that is not an array is unchanged (even if the element is an object that has instance method flatten
).
With non-negative integer argument depth
, flattens recursively through depth
levels:
a = [ 0, [ 1, [2, 3], 4 ], 5, {foo: 0}, Set.new([6, 7]) ] a # => [0, [1, [2, 3], 4], 5, {:foo=>0}, #<Set: {6, 7}>] a.flatten(0) # => [0, [1, [2, 3], 4], 5, {:foo=>0}, #<Set: {6, 7}>] a.flatten(1 ) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.flatten(1.1) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.flatten(2) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.flatten(3) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
With nil
or negative depth
, flattens all levels.
a.flatten # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.flatten(-1) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
Related: Array#flatten!
; see also Methods for Converting.
Returns self
as a recursively flattening of self
to depth
levels of recursion; depth
must be an integer-convertible object, or nil
. At each level of recursion:
Each element that is an array is “flattened” (that is, replaced by its individual array elements).
Each element that is not an array is unchanged (even if the element is an object that has instance method flatten
).
Returns nil
if no elements were flattened.
With non-negative integer argument depth
, flattens recursively through depth
levels:
a = [ 0, [ 1, [2, 3], 4 ], 5, {foo: 0}, Set.new([6, 7]) ] a # => [0, [1, [2, 3], 4], 5, {:foo=>0}, #<Set: {6, 7}>] a.dup.flatten!(1) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.dup.flatten!(1.1) # => [0, 1, [2, 3], 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.dup.flatten!(2) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.dup.flatten!(3) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
With nil
or negative argument depth
, flattens all levels:
a.dup.flatten! # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>] a.dup.flatten!(-1) # => [0, 1, 2, 3, 4, 5, {:foo=>0}, #<Set: {6, 7}>]
Related: Array#flatten
; see also Methods for Assigning.