Adds to array
all elements from each Array
in other_arrays
; returns self
:
a = [0, 1] a.concat([2, 3], [4, 5]) # => [0, 1, 2, 3, 4, 5]
Inserts given objects
before or after the element at Integer
index offset
; returns self
.
When index
is non-negative, inserts all given objects
before the element at offset index
:
a = [:foo, 'bar', 2] a.insert(1, :bat, :bam) # => [:foo, :bat, :bam, "bar", 2]
Extends the array if index
is beyond the array (index >= self.size
):
a = [:foo, 'bar', 2] a.insert(5, :bat, :bam) a # => [:foo, "bar", 2, nil, nil, :bat, :bam]
Does nothing if no objects given:
a = [:foo, 'bar', 2] a.insert(1) a.insert(50) a.insert(-50) a # => [:foo, "bar", 2]
When index
is negative, inserts all given objects
after the element at offset index+self.size
:
a = [:foo, 'bar', 2] a.insert(-2, :bat, :bam) a # => [:foo, "bar", :bat, :bam, 2]
Returns a new Array
whose elements are those from self
, sorted.
With no block, compares elements using operator <=>
(see Comparable
):
a = 'abcde'.split('').shuffle a # => ["e", "b", "d", "a", "c"] a1 = a.sort a1 # => ["a", "b", "c", "d", "e"]
With a block, calls the block with each element pair; for each element pair a
and b
, the block should return an integer:
Negative when b
is to follow a
.
Zero when a
and b
are equivalent.
Positive when a
is to follow b
.
Example:
a = 'abcde'.split('').shuffle a # => ["e", "b", "d", "a", "c"] a1 = a.sort {|a, b| a <=> b } a1 # => ["a", "b", "c", "d", "e"] a2 = a.sort {|a, b| b <=> a } a2 # => ["e", "d", "c", "b", "a"]
When the block returns zero, the order for a
and b
is indeterminate, and may be unstable:
a = 'abcde'.split('').shuffle a # => ["e", "b", "d", "a", "c"] a1 = a.sort {|a, b| 0 } a1 # => ["c", "e", "b", "d", "a"]
Related: Enumerable#sort_by
.
Returns self
with its elements sorted in place.
With no block, compares elements using operator <=>
(see Comparable
):
a = 'abcde'.split('').shuffle a # => ["e", "b", "d", "a", "c"] a.sort! a # => ["a", "b", "c", "d", "e"]
With a block, calls the block with each element pair; for each element pair a
and b
, the block should return an integer:
Negative when b
is to follow a
.
Zero when a
and b
are equivalent.
Positive when a
is to follow b
.
Example:
a = 'abcde'.split('').shuffle a # => ["e", "b", "d", "a", "c"] a.sort! {|a, b| a <=> b } a # => ["a", "b", "c", "d", "e"] a.sort! {|a, b| b <=> a } a # => ["e", "d", "c", "b", "a"]
When the block returns zero, the order for a
and b
is indeterminate, and may be unstable:
a = 'abcde'.split('').shuffle a # => ["e", "b", "d", "a", "c"] a.sort! {|a, b| 0 } a # => ["d", "e", "c", "a", "b"]
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 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?
Formats each element in self
into a binary string; returns that string. See Packed Data.
Returns the integer square root of the non-negative integer n
, which is the largest non-negative integer less than or equal to the square root of numeric
.
Integer.sqrt(0) # => 0 Integer.sqrt(1) # => 1 Integer.sqrt(24) # => 4 Integer.sqrt(25) # => 5 Integer.sqrt(10**400) # => 10**200
If numeric
is not an Integer, it is converted to an Integer:
Integer.sqrt(Complex(4, 0)) # => 2 Integer.sqrt(Rational(4, 1)) # => 2 Integer.sqrt(4.0) # => 2 Integer.sqrt(3.14159) # => 1
This method is equivalent to Math.sqrt(numeric).floor
, except that the result of the latter code may differ from the true value due to the limited precision of floating point arithmetic.
Integer.sqrt(10**46) # => 100000000000000000000000 Math.sqrt(10**46).floor # => 99999999999999991611392
Raises an exception if numeric
is negative.
Returns true
if all bits that are set (=1) in mask
are also set in self
; returns false
otherwise.
Example values:
0b1010101 self 0b1010100 mask 0b1010100 self & mask true self.allbits?(mask) 0b1010100 self 0b1010101 mask 0b1010100 self & mask false self.allbits?(mask)
Related: Integer#anybits?
, Integer#nobits?
.
Returns true
if any bit that is set (=1) in mask
is also set in self
; returns false
otherwise.
Example values:
0b10000010 self 0b11111111 mask 0b10000010 self & mask true self.anybits?(mask) 0b00000000 self 0b11111111 mask 0b00000000 self & mask false self.anybits?(mask)
Related: Integer#allbits?
, Integer#nobits?
.
Returns true
if no bit that is set (=1) in mask
is also set in self
; returns false
otherwise.
Example values:
0b11110000 self 0b00001111 mask 0b00000000 self & mask true self.nobits?(mask) 0b00000001 self 0b11111111 mask 0b00000001 self & mask false self.nobits?(mask)
Related: Integer#allbits?
, Integer#anybits?
.
Returns an array of integers representing the base
-radix digits of self
; the first element of the array represents the least significant digit:
12345.digits # => [5, 4, 3, 2, 1] 12345.digits(7) # => [4, 6, 6, 0, 5] 12345.digits(100) # => [45, 23, 1]
Raises an exception if self
is negative or base
is less than 2.
Return the class or module refined by the receiver.
module M refine String do end end M.refinements[0].target # => String
Returns a new Complex object formed from the arguments, each of which must be an instance of Numeric
, or an instance of one of its subclasses: Complex, Float
, Integer
, Rational
; see Rectangular Coordinates:
Complex.rect(3) # => (3+0i) Complex.rect(3, Math::PI) # => (3+3.141592653589793i) Complex.rect(-3, -Math::PI) # => (-3-3.141592653589793i)
Complex.rectangular is an alias for Complex.rect.
Returns a new Complex object formed from the arguments, each of which must be an instance of Numeric
, or an instance of one of its subclasses: Complex, Float
, Integer
, Rational
. Argument arg
is given in radians; see Polar Coordinates:
Complex.polar(3) # => (3+0i) Complex.polar(3, 2.0) # => (-1.2484405096414273+2.727892280477045i) Complex.polar(-3, -2.0) # => (1.2484405096414273+2.727892280477045i)
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 the argument (angle) for self
in radians; see polar coordinates:
Complex.polar(3, Math::PI/2).arg # => 1.57079632679489660
If self
was created with rectangular coordinates, the returned value is computed, and may be inexact:
Complex.polar(1, 1.0/3).arg # => 0.33333333333333326
Returns the array [self.real, self.imag]
:
Complex.rect(1, 2).rect # => [1, 2]
If self
was created with polar coordinates, the returned value is computed, and may be inexact:
Complex.polar(1.0, 1.0).rect # => [0.5403023058681398, 0.8414709848078965]
Complex#rectangular
is an alias for Complex#rect
.
Returns the array [self.abs, self.arg]
:
Complex.polar(1, 2).polar # => [1.0, 2.0]
See Polar Coordinates.
If self
was created with rectangular coordinates, the returned value is computed, and may be inexact:
Complex.rect(1, 1).polar # => [1.4142135623730951, 0.7853981633974483]