Calls the block, if given, with each element of self; returns a new Array containing those elements of self for which the block returns a truthy value:
a = [:foo, 'bar', 2, :bam] a1 = a.select {|element| element.to_s.start_with?('b') } a1 # => ["bar", :bam]
Returns a new Enumerator if no block given:
a = [:foo, 'bar', 2, :bam] a.select # => #<Enumerator: [:foo, "bar", 2, :bam]:select>
Calls the block, if given with each element of self; removes from self those elements for which the block returns false or nil.
Returns self if any elements were removed:
a = [:foo, 'bar', 2, :bam] a.select! {|element| element.to_s.start_with?('b') } # => ["bar", :bam]
Returns nil if no elements were removed.
Returns a new Enumerator if no block given:
a = [:foo, 'bar', 2, :bam] a.select! # => #<Enumerator: [:foo, "bar", 2, :bam]:select!>
Transposes the rows and columns in an Array of Arrays; the nested Arrays must all be the same size:
a = [[:a0, :a1], [:b0, :b1], [:c0, :c1]] a.transpose # => [[:a0, :b0, :c0], [:a1, :b1, :c1]]
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 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 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?
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.
Formats each element in self into a binary string; returns that string. See Packed Data.
Returns an array with both a numeric and a int represented as Integer objects or Float objects.
This is achieved by converting numeric to an Integer or a Float.
A TypeError is raised if the numeric is not an Integer or a Float type.
(0x3FFFFFFFFFFFFFFF+1).coerce(42) #=> [42, 4611686018427387904]
Calls the given block with each integer value from self up to limit; returns self:
a = [] 5.upto(10) {|i| a << i } # => 5 a # => [5, 6, 7, 8, 9, 10] a = [] -5.upto(0) {|i| a << i } # => -5 a # => [-5, -4, -3, -2, -1, 0] 5.upto(4) {|i| fail 'Cannot happen' } # => 5
With no block given, returns an Enumerator.
Returns the remainder after dividing self by other.
Examples:
11.remainder(4) # => 3 11.remainder(-4) # => 3 -11.remainder(4) # => -3 -11.remainder(-4) # => -3 12.remainder(4) # => 0 12.remainder(-4) # => 0 -12.remainder(4) # => 0 -12.remainder(-4) # => 0 13.remainder(4.0) # => 1.0 13.remainder(Rational(4, 1)) # => (1/1)
Since self is already an Integer, always returns true.
Calls the given block self times with each integer in (0..self-1):
a = [] 5.times {|i| a.push(i) } # => 5 a # => [0, 1, 2, 3, 4]
With no block given, returns an Enumerator.
Returns true if self has a zero value, false otherwise.
Returns self.
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(7).imaginary #=> 0 Complex(9, -4).imaginary #=> -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 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 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.