Attempts to return an array, based on the given object
.
If object
is an array, returns object
.
Otherwise if object
responds to :to_ary
. calls object.to_ary
: if the return value is an array or nil
, returns that value; if not, raises TypeError
.
Otherwise returns nil
.
Related: see Methods for Creating an Array.
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.
When self
is an instance of Array, returns self
.
Otherwise, returns a new array containing the elements of self
:
class MyArray < Array; end my_a = MyArray.new(['foo', 'bar', 'two']) a = my_a.to_a a # => ["foo", "bar", "two"] a.class # => Array # Not MyArray.
Related: see Methods for Converting.
Returns a new hash formed from self
.
With no block given, each element of self
must be a 2-element sub-array; forms each sub-array into a key-value pair in the new hash:
a = [['foo', 'zero'], ['bar', 'one'], ['baz', 'two']] a.to_h # => {"foo"=>"zero", "bar"=>"one", "baz"=>"two"} [].to_h # => {}
With a block given, the block must return a 2-element array; calls the block with each element of self
; forms each returned array into a key-value pair in the returned hash:
a = ['foo', :bar, 1, [2, 3], {baz: 4}] a.to_h {|element| [element, element.class] } # => {"foo"=>String, :bar=>Symbol, 1=>Integer, [2, 3]=>Array, {:baz=>4}=>Hash}
Related: see Methods for Converting.
With a block given, iterates over the elements of self
, passing each array index to the block; returns self
:
a = [:foo, 'bar', 2] a.each_index {|index| puts "#{index} #{a[index]}" }
Output:
0 foo 1 bar 2 2
Allows the array to be modified during iteration:
a = [:foo, 'bar', 2] a.each_index {|index| puts index; a.clear if index > 0 } a # => []
Output:
0 1
With no block given, returns a new Enumerator
.
Related: see Methods for Iterating.
When a block given, iterates backwards over the elements of self
, passing, in reverse order, each element to the block; returns self
:
a = [] [0, 1, 2].reverse_each {|element| a.push(element) } a # => [2, 1, 0]
Allows the array to be modified during iteration:
a = ['a', 'b', 'c'] a.reverse_each {|element| a.clear if element.start_with?('b') } a # => []
When no block given, returns a new Enumerator
.
Related: see Methods for Iterating.
Returns the zero-based integer index of a specified element, or nil
.
With only argument object
given, returns the index of the first element element
for which object == element
:
a = [:foo, 'bar', 2, 'bar'] a.index('bar') # => 1
Returns nil
if no such element found.
With only a block given, calls the block with each successive element; returns the index of the first element for which the block returns a truthy value:
a = [:foo, 'bar', 2, 'bar'] a.index {|element| element == 'bar' } # => 1
Returns nil
if the block never returns a truthy value.
With neither an argument nor a block given, returns a new Enumerator
.
Related: see Methods for Querying.
With a block given, sorts the elements of self
in place; returns self.
Calls the block with each successive element; sorts elements based on the values returned from the block:
a = ['aaaa', 'bbb', 'cc', 'd'] a.sort_by! {|element| element.size } a # => ["d", "cc", "bbb", "aaaa"]
For duplicate values returned by the block, the ordering is indeterminate, and may be unstable.
With no block given, returns a new Enumerator
.
Related: see Methods for Assigning.
With a block given, calls the block with each element of self
; removes the element from self
if the block does not return a truthy value:
a = [:foo, 'bar', 2, :bam] a.keep_if {|element| element.to_s.start_with?('b') } # => ["bar", :bam]
With no block given, returns a new Enumerator
.
Related: see Methods for Deleting.
Returns elements from self
in a new array; does not modify self
.
The objects included in the returned array are the elements of self
selected by the given specifiers
, each of which must be a numeric index or a Range
.
In brief:
a = ['a', 'b', 'c', 'd'] # Index specifiers. a.values_at(2, 0, 2, 0) # => ["c", "a", "c", "a"] # May repeat. a.values_at(-4, -3, -2, -1) # => ["a", "b", "c", "d"] # Counts backwards if negative. a.values_at(-50, 50) # => [nil, nil] # Outside of self. # Range specifiers. a.values_at(1..3) # => ["b", "c", "d"] # From range.begin to range.end. a.values_at(1...3) # => ["b", "c"] # End excluded. a.values_at(3..1) # => [] # No such elements. a.values_at(-3..3) # => ["b", "c", "d"] # Negative range.begin counts backwards. a.values_at(-50..3) # Raises RangeError. a.values_at(1..-2) # => ["b", "c"] # Negative range.end counts backwards. a.values_at(1..-50) # => [] # No such elements. # Mixture of specifiers. a.values_at(2..3, 3, 0..1, 0) # => ["c", "d", "d", "a", "b", "a"]
With no specifiers
given, returns a new empty array:
a = ['a', 'b', 'c', 'd'] a.values_at # => []
For each numeric specifier index
, includes an element:
For each non-negative numeric specifier index
that is in-range (less than self.size
), includes the element at offset index
:
a.values_at(0, 2) # => ["a", "c"] a.values_at(0.1, 2.9) # => ["a", "c"]
For each negative numeric index
that is in-range (greater than or equal to - self.size
), counts backwards from the end of self
:
a.values_at(-1, -4) # => ["d", "a"]
The given indexes may be in any order, and may repeat:
a.values_at(2, 0, 1, 0, 2) # => ["c", "a", "b", "a", "c"]
For each index
that is out-of-range, includes nil
:
a.values_at(4, -5) # => [nil, nil]
For each Range
specifier range
, includes elements according to range.begin
and range.end
:
If both range.begin
and range.end
are non-negative and in-range (less than self.size
), includes elements from index range.begin
through range.end - 1
(if range.exclude_end?
), or through range.end
(otherwise):
a.values_at(1..2) # => ["b", "c"] a.values_at(1...2) # => ["b"]
If range.begin
is negative and in-range (greater than or equal to - self.size
), counts backwards from the end of self
:
a.values_at(-2..3) # => ["c", "d"]
If range.begin
is negative and out-of-range, raises an exception:
a.values_at(-5..3) # Raises RangeError.
If range.end
is positive and out-of-range, extends the returned array with nil
elements:
a.values_at(1..5) # => ["b", "c", "d", nil, nil]
If range.end
is negative and in-range, counts backwards from the end of self
:
a.values_at(1..-2) # => ["b", "c"]
If range.end
is negative and out-of-range, returns an empty array:
a.values_at(1..-5) # => []
The given ranges may be in any order and may repeat:
a.values_at(2..3, 0..1, 2..3) # => ["c", "d", "a", "b", "c", "d"]
The given specifiers may be any mixture of indexes and ranges:
a.values_at(3, 1..2, 0, 2..3) # => ["d", "b", "c", "a", "c", "d"]
Related: see Methods for Fetching.
Removes the element of self
at the given index
, which must be an integer-convertible object.
When index
is non-negative, deletes the element at offset index
:
a = [:foo, 'bar', 2] a.delete_at(1) # => "bar" a # => [:foo, 2]
When index
is negative, counts backward from the end of the array:
a = [:foo, 'bar', 2] a.delete_at(-2) # => "bar" a # => [:foo, 2]
When index
is out of range, returns nil
.
a = [:foo, 'bar', 2] a.delete_at(3) # => nil a.delete_at(-4) # => nil
Related: see Methods for Deleting.
With a block given, calls the block with each element of self
; removes the element if the block returns a truthy value; returns self
:
a = [:foo, 'bar', 2, 'bat'] a.delete_if {|element| element.to_s.start_with?('b') } # => [:foo, 2]
With no block given, returns a new Enumerator
.
Related: see Methods for Deleting.
With a block given, calls the block with each repeated permutation of length size
of the elements of self
; each permutation is an array; returns self
. The order of the permutations is indeterminate.
If a positive integer argument size
is given, calls the block with each size
-tuple repeated permutation of the elements of self
. The number of permutations is self.size**size
.
Examples:
size
is 1:
p = [] [0, 1, 2].repeated_permutation(1) {|permutation| p.push(permutation) } p # => [[0], [1], [2]]
size
is 2:
p = [] [0, 1, 2].repeated_permutation(2) {|permutation| p.push(permutation) } p # => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
If size
is zero, calls the block once with an empty array.
If size
is negative, does not call the block:
[0, 1, 2].repeated_permutation(-1) {|permutation| fail 'Cannot happen' }
With no block given, returns a new Enumerator
.
Related: see Methods for Combining.
With a block given, calls the block with each repeated combination of length size
of the elements of self
; each combination is an array; returns self
. The order of the combinations is indeterminate.
If a positive integer argument size
is given, calls the block with each size
-tuple repeated combination of the elements of self
. The number of combinations is (size+1)(size+2)/2
.
Examples:
size
is 1:
c = [] [0, 1, 2].repeated_combination(1) {|combination| c.push(combination) } c # => [[0], [1], [2]]
size
is 2:
c = [] [0, 1, 2].repeated_combination(2) {|combination| c.push(combination) } c # => [[0, 0], [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]]
If size
is zero, calls the block once with an empty array.
If size
is negative, does not call the block:
[0, 1, 2].repeated_combination(-1) {|combination| fail 'Cannot happen' }
With no block given, returns a new Enumerator
.
Related: see Methods for Combining.
With a block given, calls the block with each successive element of self
; stops iterating if the block returns false
or nil
; returns a new array containing those elements for which the block returned a truthy value:
a = [0, 1, 2, 3, 4, 5] a.take_while {|element| element < 3 } # => [0, 1, 2] a.take_while {|element| true } # => [0, 1, 2, 3, 4, 5] a.take_while {|element| false } # => []
With no block given, returns a new Enumerator
.
Does not modify self
.
Related: see Methods for Fetching.
With a block given, calls the block with each successive element of self
; stops if the block returns false
or nil
; returns a new array omitting those elements for which the block returned a truthy value; does not modify self
:
a = [0, 1, 2, 3, 4, 5] a.drop_while {|element| element < 3 } # => [3, 4, 5]
With no block given, returns a new Enumerator
.
Related: see Methods for Fetching.
Returns the new string formed by calling method #inspect
on each array element:
a = [:foo, 'bar', 2] a.inspect # => "[:foo, \"bar\", 2]"
Related: see Methods for Converting.
With no block given, returns a new array containing the elements of self
at the offsets specified by indexes
. Each of the indexes
must be an integer-convertible object:
a = [:foo, :bar, :baz] a.fetch_values(2, 0) # => [:baz, :foo] a.fetch_values(2.1, 0) # => [:baz, :foo] a.fetch_values # => []
For a negative index, counts backwards from the end of the array:
a.fetch_values(-2, -1) # [:bar, :baz]
When no block is given, raises an exception if any index is out of range.
With a block given, for each index:
If the index is in range, uses an element of self
(as above).
Otherwise, calls the block with the index and uses the block’s return value.
Example:
a = [:foo, :bar, :baz] a.fetch_values(1, 0, 42, 777) { |index| index.to_s } # => [:bar, :foo, "42", "777"]
Related: see Methods for Fetching.
in [foo, bar, baz]