Returns whether other_array
has at least one element that is #eql?
to some element of self
:
[1, 2, 3].intersect?([3, 4, 5]) # => true [1, 2, 3].intersect?([4, 5, 6]) # => false
Each element must correctly implement method #hash
.
Related: see Methods for Querying.
Inserts the given objects
as elements of self
; returns self
.
When index
is non-negative, inserts objects
before the element at offset index
:
a = ['a', 'b', 'c'] # => ["a", "b", "c"] a.insert(1, :x, :y, :z) # => ["a", :x, :y, :z, "b", "c"]
Extends the array if index
is beyond the array (index >= self.size
):
a = ['a', 'b', 'c'] # => ["a", "b", "c"] a.insert(5, :x, :y, :z) # => ["a", "b", "c", nil, nil, :x, :y, :z]
When index
is negative, inserts objects
after the element at offset index + self.size
:
a = ['a', 'b', 'c'] # => ["a", "b", "c"] a.insert(-2, :x, :y, :z) # => ["a", "b", :x, :y, :z, "c"]
With no objects
given, does nothing:
a = ['a', 'b', 'c'] # => ["a", "b", "c"] a.insert(1) # => ["a", "b", "c"] a.insert(50) # => ["a", "b", "c"] a.insert(-50) # => ["a", "b", "c"]
Raises IndexError
if objects
are given and index
is negative and out of range.
Related: see Methods for Assigning.
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.
Returns the index of the last element for which object == element
.
With argument object
given, returns the index of the last such element found:
a = [:foo, 'bar', 2, 'bar'] a.rindex('bar') # => 3
Returns nil
if no such object found.
With a block given, calls the block with each successive element; returns the index of the last element for which the block returns a truthy value:
a = [:foo, 'bar', 2, 'bar'] a.rindex {|element| element == 'bar' } # => 3
Returns nil
if the block never returns a truthy value.
When neither an argument nor a block is given, returns a new Enumerator
.
Related: see Methods for Querying.
Returns the new string formed by joining the converted elements of self
; for each element element
:
Converts recursively using element.join(separator)
if element
is a kind_of?(Array)
.
Otherwise, converts using element.to_s
.
With no argument given, joins using the output field separator, $,
:
a = [:foo, 'bar', 2] $, # => nil a.join # => "foobar2"
With string argument separator
given, joins using that separator:
a = [:foo, 'bar', 2] a.join("\n") # => "foo\nbar\n2"
Joins recursively for nested arrays:
a = [:foo, [:bar, [:baz, :bat]]] a.join # => "foobarbazbat"
Related: see Methods for Converting.
With a block given, calls the block with each element of self
; returns a new array whose elements are the return values from the block:
a = [:foo, 'bar', 2] a1 = a.map {|element| element.class } a1 # => [Symbol, String, Integer]
With no block given, returns a new Enumerator
.
Related: collect!
; see also Methods for Converting.
With a block given, calls the block with each element of self
and replaces the element with the block’s return value; returns self
:
a = [:foo, 'bar', 2] a.map! { |element| element.class } # => [Symbol, String, Integer]
With no block given, returns a new Enumerator
.
Related: collect
; see also Methods for Converting.
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.
When a block and a positive integer-convertible object argument count
(0 < count <= self.size
) are given, calls the block with each combination of self
of size count
; returns self
:
a = %w[a b c] # => ["a", "b", "c"] a.combination(2) {|combination| p combination } # => ["a", "b", "c"]
Output:
["a", "b"] ["a", "c"] ["b", "c"]
The order of the yielded combinations is not guaranteed.
When count
is zero, calls the block once with a new empty array:
a.combination(0) {|combination| p combination } [].combination(0) {|combination| p combination }
Output:
[] []
When count
is negative or larger than self.size
and self
is non-empty, does not call the block:
a.combination(-1) {|combination| fail 'Cannot happen' } # => ["a", "b", "c"] a.combination(4) {|combination| fail 'Cannot happen' } # => ["a", "b", "c"]
With no block given, returns a new Enumerator
.
Related: Array#permutation
; see also Methods for Iterating.
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.
Returns a string containing the place-value representation of self
in radix base
(in 2..36).
12345.to_s # => "12345" 12345.to_s(2) # => "11000000111001" 12345.to_s(8) # => "30071" 12345.to_s(10) # => "12345" 12345.to_s(16) # => "3039" 12345.to_s(36) # => "9ix" 78546939656932.to_s(36) # => "rubyrules"
Raises an exception if base
is out of range.
Since self
is already an Integer, always returns true
.
Returns the imaginary value for self
:
Complex.rect(7).imag # => 0 Complex.rect(9, -4).imag # => -4
If self
was created with polar coordinates, the returned value is computed, and may be inexact:
Complex.polar(1, Math::PI/4).imag # => 0.7071067811865476 # Square root of 2.
Returns the absolute value (magnitude) for self
; see polar coordinates:
Complex.polar(-1, 0).abs # => 1.0
If self
was created with rectangular coordinates, the returned value is computed, and may be inexact:
Complex.rectangular(1, 1).abs # => 1.4142135623730951 # The square root of 2.
Returns a string representation of self
:
Complex.rect(2).inspect # => "(2+0i)" Complex.rect(-8, 6).inspect # => "(-8+6i)" Complex.rect(0, Rational(1, 2)).inspect # => "(0+(1/2)*i)" Complex.rect(0, Float::INFINITY).inspect # => "(0+Infinity*i)" Complex.rect(Float::NAN, Float::NAN).inspect # => "(NaN+NaN*i)"
Returns true
if both self.real.finite?
and self.imag.finite?
are true, false
otherwise:
Complex.rect(1, 1).finite? # => true Complex.rect(Float::INFINITY, 0).finite? # => false
Related: Numeric#finite?
, Float#finite?
.
Returns the absolute value of self
.
12.abs #=> 12 (-34.56).abs #=> 34.56 -34.56.abs #=> 34.56
Returns true
if self
is a finite number, false
otherwise.
Inserts the given other_string
into self
; returns self
.
If the Integer
index
is positive, inserts other_string
at offset index
:
'foo'.insert(1, 'bar') # => "fbaroo"
If the Integer
index
is negative, counts backward from the end of self
and inserts other_string
at offset index+1
(that is, after self[index]
):
'foo'.insert(-2, 'bar') # => "fobaro"
Returns a MatchData
object (or nil
) based on self
and the given pattern
.
Note: also updates Global Variables at Regexp
.
Computes regexp
by converting pattern
(if not already a Regexp
).
regexp = Regexp.new(pattern)
Computes matchdata
, which will be either a MatchData
object or nil
(see Regexp#match
):
matchdata = regexp.match(self)
With no block given, returns the computed matchdata
:
'foo'.match('f') # => #<MatchData "f"> 'foo'.match('o') # => #<MatchData "o"> 'foo'.match('x') # => nil
If Integer
argument offset
is given, the search begins at index offset
:
'foo'.match('f', 1) # => nil 'foo'.match('o', 1) # => #<MatchData "o">
With a block given, calls the block with the computed matchdata
and returns the block’s return value:
'foo'.match(/o/) {|matchdata| matchdata } # => #<MatchData "o"> 'foo'.match(/x/) {|matchdata| matchdata } # => nil 'foo'.match(/f/, 1) {|matchdata| matchdata } # => nil
Returns true
or false
based on whether a match is found for self
and pattern
.
Note: does not update Global Variables at Regexp
.
Computes regexp
by converting pattern
(if not already a Regexp
).
regexp = Regexp.new(pattern)
Returns true
if self+.match(regexp)
returns a MatchData
object, false
otherwise:
'foo'.match?(/o/) # => true 'foo'.match?('o') # => true 'foo'.match?(/x/) # => false
If Integer
argument offset
is given, the search begins at index offset
:
'foo'.match?('f', 1) # => false 'foo'.match?('o', 1) # => true