Returns a count of specified elements.
With no argument and no block, returns the count of all elements:
[0, 1, 2].count # => 3 [].count # => 0
With argument obj
, returns the count of elements ==
to obj
:
[0, 1, 2, 0.0].count(0) # => 2 [0, 1, 2].count(3) # => 0
With no argument and a block given, calls the block with each element; returns the count of elements for which the block returns a truthy value:
[0, 1, 2, 3].count {|element| element > 1} # => 2
With argument obj
and a block given, issues a warning, ignores the block, and returns the count of elements ==
to obj
.
When called with positive Integer
argument count
and a block, calls the block with each element, then does so again, until it has done so count
times; returns nil
:
output = [] [0, 1].cycle(2) {|element| output.push(element) } # => nil output # => [0, 1, 0, 1]
If count
is zero or negative, does not call the block:
[0, 1].cycle(0) {|element| fail 'Cannot happen' } # => nil [0, 1].cycle(-1) {|element| fail 'Cannot happen' } # => nil
When a block is given, and argument is omitted or nil
, cycles forever:
# Prints 0 and 1 forever. [0, 1].cycle {|element| puts element } [0, 1].cycle(nil) {|element| puts element }
When no block is given, returns a new Enumerator:
[0, 1].cycle(2) # => #<Enumerator: [0, 1]:cycle(2)> [0, 1].cycle # => # => #<Enumerator: [0, 1]:cycle> [0, 1].cycle.first(5) # => [0, 1, 0, 1, 0]
When invoked with a block, yield all permutations of elements of self
; returns self
. The order of permutations is indeterminate.
When a block and an in-range positive Integer
argument n
(0 < n <= self.size
) are given, calls the block with all n
-tuple permutations of self
.
Example:
a = [0, 1, 2] a.permutation(2) {|permutation| p permutation }
Output:
[0, 1] [0, 2] [1, 0] [1, 2] [2, 0] [2, 1]
Another example:
a = [0, 1, 2] a.permutation(3) {|permutation| p permutation }
Output:
[0, 1, 2] [0, 2, 1] [1, 0, 2] [1, 2, 0] [2, 0, 1] [2, 1, 0]
When n
is zero, calls the block once with a new empty Array:
a = [0, 1, 2] a.permutation(0) {|permutation| p permutation }
Output:
[]
When n
is out of range (negative or larger than self.size
), does not call the block:
a = [0, 1, 2] a.permutation(-1) {|permutation| fail 'Cannot happen' } a.permutation(4) {|permutation| fail 'Cannot happen' }
When a block given but no argument, behaves the same as a.permutation(a.size)
:
a = [0, 1, 2] a.permutation {|permutation| p permutation }
Output:
[0, 1, 2] [0, 2, 1] [1, 0, 2] [1, 2, 0] [2, 0, 1] [2, 1, 0]
Returns a new Enumerator
if no block given:
a = [0, 1, 2] a.permutation # => #<Enumerator: [0, 1, 2]:permutation> a.permutation(2) # => #<Enumerator: [0, 1, 2]:permutation(2)>
Calls the block, if given, with combinations of elements of self
; returns self
. The order of combinations is indeterminate.
When a block and an in-range positive Integer
argument n
(0 < n <= self.size
) are given, calls the block with all n
-tuple combinations of self
.
Example:
a = [0, 1, 2] a.combination(2) {|combination| p combination }
Output:
[0, 1] [0, 2] [1, 2]
Another example:
a = [0, 1, 2] a.combination(3) {|combination| p combination }
Output:
[0, 1, 2]
When n
is zero, calls the block once with a new empty Array:
a = [0, 1, 2] a1 = a.combination(0) {|combination| p combination }
Output:
[]
When n
is out of range (negative or larger than self.size
), does not call the block:
a = [0, 1, 2] a.combination(-1) {|combination| fail 'Cannot happen' } a.combination(4) {|combination| fail 'Cannot happen' }
Returns a new Enumerator
if no block given:
a = [0, 1, 2] a.combination(2) # => #<Enumerator: [0, 1, 2]:combination(2)>
Computes and returns or yields all combinations of elements from all the Arrays, including both self
and other_arrays
:
The number of combinations is the product of the sizes of all the arrays, including both self
and other_arrays
.
The order of the returned combinations is indeterminate.
When no block is given, returns the combinations as an Array of Arrays:
a = [0, 1, 2] a1 = [3, 4] a2 = [5, 6] p = a.product(a1) p.size # => 6 # a.size * a1.size p # => [[0, 3], [0, 4], [1, 3], [1, 4], [2, 3], [2, 4]] p = a.product(a1, a2) p.size # => 12 # a.size * a1.size * a2.size p # => [[0, 3, 5], [0, 3, 6], [0, 4, 5], [0, 4, 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]]
If any argument is an empty Array, returns an empty Array.
If no argument is given, returns an Array of 1-element Arrays, each containing an element of self
:
a.product # => [[0], [1], [2]]
When a block is given, yields each combination as an Array; returns self
:
a.product(a1) {|combination| p combination }
Output:
[0, 3] [0, 4] [1, 3] [1, 4] [2, 3] [2, 4]
If any argument is an empty Array, does not call the block:
a.product(a1, a2, []) {|combination| fail 'Cannot happen' }
If no argument is given, yields each element of self
as a 1-element Array:
a.product {|combination| p combination }
Output:
[0] [1] [2]
Returns a new Array containing the first n
element of self
, where n
is a non-negative Integer
; does not modify self
.
Examples:
a = [0, 1, 2, 3, 4, 5] a.take(1) # => [0] a.take(2) # => [0, 1] a.take(50) # => [0, 1, 2, 3, 4, 5] a # => [0, 1, 2, 3, 4, 5]
Returns a new Array containing all but the first n
element of self
, where n
is a non-negative Integer
; does not modify self
.
Examples:
a = [0, 1, 2, 3, 4, 5] a.drop(0) # => [0, 1, 2, 3, 4, 5] a.drop(1) # => [1, 2, 3, 4, 5] a.drop(2) # => [2, 3, 4, 5]
Returns true
if any element of self
meets a given criterion.
If self
has no element, returns false
and argument or block are not used.
With no block given and no argument, returns true
if self
has any truthy element, false
otherwise:
[nil, 0, false].any? # => true [nil, false].any? # => false [].any? # => false
With a block given and no argument, calls the block with each element in self
; returns true
if the block returns any truthy value, false
otherwise:
[0, 1, 2].any? {|element| element > 1 } # => true [0, 1, 2].any? {|element| element > 2 } # => false
If argument obj
is given, returns true
if obj
.===
any element, false
otherwise:
['food', 'drink'].any?(/foo/) # => true ['food', 'drink'].any?(/bar/) # => false [].any?(/foo/) # => false [0, 1, 2].any?(1) # => true [0, 1, 2].any?(3) # => false
Related: Enumerable#any?
Returns true
if all elements of self
meet a given criterion.
If self
has no element, returns true
and argument or block are not used.
With no block given and no argument, returns true
if self
contains only truthy elements, false
otherwise:
[0, 1, :foo].all? # => true [0, nil, 2].all? # => false [].all? # => true
With a block given and no argument, calls the block with each element in self
; returns true
if the block returns only truthy values, false
otherwise:
[0, 1, 2].all? { |element| element < 3 } # => true [0, 1, 2].all? { |element| element < 2 } # => false
If argument obj
is given, returns true
if obj.===
every element, false
otherwise:
['food', 'fool', 'foot'].all?(/foo/) # => true ['food', 'drink'].all?(/bar/) # => false [].all?(/foo/) # => true [0, 0, 0].all?(0) # => true [0, 1, 2].all?(1) # => false
Related: Enumerable#all?
Returns true
if no element of self
meet a given criterion.
With no block given and no argument, returns true
if self
has no truthy elements, false
otherwise:
[nil, false].none? # => true [nil, 0, false].none? # => false [].none? # => true
With a block given and no argument, calls the block with each element in self
; returns true
if the block returns no truthy value, false
otherwise:
[0, 1, 2].none? {|element| element > 3 } # => true [0, 1, 2].none? {|element| element > 1 } # => false
If argument obj
is given, returns true
if obj.===
no element, false
otherwise:
['food', 'drink'].none?(/bar/) # => true ['food', 'drink'].none?(/foo/) # => false [].none?(/foo/) # => true [0, 1, 2].none?(3) # => true [0, 1, 2].none?(1) # => false
Related: Enumerable#none?
Returns true
if exactly one element of self
meets a given criterion.
With no block given and no argument, returns true
if self
has exactly one truthy element, false
otherwise:
[nil, 0].one? # => true [0, 0].one? # => false [nil, nil].one? # => false [].one? # => false
With a block given and no argument, calls the block with each element in self
; returns true
if the block a truthy value for exactly one element, false
otherwise:
[0, 1, 2].one? {|element| element > 0 } # => false [0, 1, 2].one? {|element| element > 1 } # => true [0, 1, 2].one? {|element| element > 2 } # => false
If argument obj
is given, returns true
if obj.===
exactly one element, false
otherwise:
[0, 1, 2].one?(0) # => true [0, 0, 1].one?(0) # => false [1, 1, 2].one?(0) # => false ['food', 'drink'].one?(/bar/) # => false ['food', 'drink'].one?(/foo/) # => true [].one?(/foo/) # => false
Related: Enumerable#one?
Finds and returns the object in nested objects that is specified by index
and identifiers
. The nested objects may be instances of various classes. See Dig Methods.
Examples:
a = [:foo, [:bar, :baz, [:bat, :bam]]] a.dig(1) # => [:bar, :baz, [:bat, :bam]] a.dig(1, 2) # => [:bat, :bam] a.dig(1, 2, 0) # => :bat a.dig(1, 2, 3) # => nil
When no block is given, returns the object equivalent to:
sum = init array.each {|element| sum += element } sum
For example, [e1, e2, e3].sum
returns init + e1 + e2 + e3
.
Examples:
a = [0, 1, 2, 3] a.sum # => 6 a.sum(100) # => 106
The elements need not be numeric, but must be +
-compatible with each other and with init
:
a = ['abc', 'def', 'ghi'] a.sum('jkl') # => "jklabcdefghi"
When a block is given, it is called with each element and the block’s return value (instead of the element itself) is used as the addend:
a = ['zero', 1, :two] s = a.sum('Coerced and concatenated: ') {|element| element.to_s } s # => "Coerced and concatenated: zero1two"
Notes:
Array#join
and Array#flatten
may be faster than Array#sum
for an Array of Strings or an Array of Arrays.
Array#sum
method may not respect method redefinition of “+” methods such as Integer#+
.
Appends trailing elements.
Appends each argument in objects
to self
; returns self
:
a = [:foo, 'bar', 2] a.push(:baz, :bat) # => [:foo, "bar", 2, :baz, :bat]
Appends each argument as one element, even if it is another Array:
a = [:foo, 'bar', 2] a1 = a.push([:baz, :bat], [:bam, :bad]) a1 # => [:foo, "bar", 2, [:baz, :bat], [:bam, :bad]]
Prepends the given objects
to self
:
a = [:foo, 'bar', 2] a.unshift(:bam, :bat) # => [:bam, :bat, :foo, "bar", 2]
Shuffles the elements of self
in place.
a = [1, 2, 3] #=> [1, 2, 3] a.shuffle! #=> [2, 3, 1] a #=> [2, 3, 1]
The optional random
argument will be used as the random number generator:
a.shuffle!(random: Random.new(1)) #=> [1, 3, 2]
Returns a new array with elements of self
shuffled.
a = [1, 2, 3] #=> [1, 2, 3] a.shuffle #=> [2, 3, 1] a #=> [1, 2, 3]
The optional random
argument will be used as the random number generator:
a.shuffle(random: Random.new(1)) #=> [1, 3, 2]
Returns random elements from self
.
When no arguments are given, returns a random element from self
:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a.sample # => 3 a.sample # => 8
If self
is empty, returns nil
.
When argument n
is given, returns a new Array containing n
random elements from self
:
a.sample(3) # => [8, 9, 2] a.sample(6) # => [9, 6, 10, 3, 1, 4]
Returns no more than a.size
elements (because no new duplicates are introduced):
a.sample(a.size * 2) # => [6, 4, 1, 8, 5, 9, 10, 2, 3, 7]
But self
may contain duplicates:
a = [1, 1, 1, 2, 2, 3] a.sample(a.size * 2) # => [1, 1, 3, 2, 1, 2]
The argument n
must be a non-negative numeric value. The order of the result array is unrelated to the order of self
. Returns a new empty Array if self
is empty.
The optional random
argument will be used as the random number generator:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a.sample(random: Random.new(1)) #=> 6 a.sample(4, random: Random.new(1)) #=> [6, 10, 9, 2]
Returns elements from self
; does not modify self
.
When no argument is given, returns the first element:
a = [:foo, 'bar', 2] a.first # => :foo a # => [:foo, "bar", 2]
If self
is empty, returns nil
.
When non-negative Integer
argument n
is given, returns the first n
elements in a new Array:
a = [:foo, 'bar', 2] a.first(2) # => [:foo, "bar"]
If n >= array.size
, returns all elements:
a = [:foo, 'bar', 2] a.first(50) # => [:foo, "bar", 2]
If n == 0
returns an new empty Array:
a = [:foo, 'bar', 2] a.first(0) # []
Related: last
.
Returns elements from self
; self
is not modified.
When no argument is given, returns the last element:
a = [:foo, 'bar', 2] a.last # => 2 a # => [:foo, "bar", 2]
If self
is empty, returns nil
.
When non-negative Integer
argument n
is given, returns the last n
elements in a new Array:
a = [:foo, 'bar', 2] a.last(2) # => ["bar", 2]
If n >= array.size
, returns all elements:
a = [:foo, 'bar', 2] a.last(50) # => [:foo, "bar", 2]
If n == 0
, returns an new empty Array:
a = [:foo, 'bar', 2] a.last(0) # []
Related: first
.
Calculates the set of unambiguous abbreviations for the strings in self
.
require 'abbrev' %w{ car cone }.abbrev #=> {"car"=>"car", "ca"=>"car", "cone"=>"cone", "con"=>"cone", "co"=>"cone"}
The optional pattern
parameter is a pattern or a string. Only input strings that match the pattern or start with the string are included in the output hash.
%w{ fast boat day }.abbrev(/^.a/) #=> {"fast"=>"fast", "fas"=>"fast", "fa"=>"fast", "day"=>"day", "da"=>"day"} Abbrev.abbrev(%w{car box cone}, "ca") #=> {"car"=>"car", "ca"=>"car"}
See also Abbrev.abbrev
Builds a command line string from an argument list array
joining all elements escaped for the Bourne shell and separated by a space.
See Shellwords.shelljoin
for details.
Formats each element in self
into a binary string; returns that string. See Packed Data.
Returns self
.
Searches self
as described at method bsearch
, but returns the index of the found element instead of the element itself.