Removes and returns trailing elements of self
.
With no argument given, removes and returns the last element, if available; otherwise returns nil
:
a = [:foo, 'bar', 2] a.pop # => 2 a # => [:foo, "bar"] [].pop # => nil
With non-negative integer argument count
given, returns a new array containing the trailing count
elements of self
, as available:
a = [:foo, 'bar', 2] a.pop(2) # => ["bar", 2] a # => [:foo] a = [:foo, 'bar', 2] a.pop(50) # => [:foo, "bar", 2] a # => []
Related: Array#push
; see also Methods for Deleting.
Returns true
if the count of elements in self
is zero, false
otherwise.
Related: see Methods for Querying.
With a block given, calls the block 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] a.select {|element| element.to_s.start_with?('b') } # => ["bar", :bam]
With no block given, returns a new Enumerator
.
Related: see Methods for Fetching.
With a block given, calls the block 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.
With no block given, returns a new Enumerator
.
Related: see Methods for Deleting.
With a block given, calls the block 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] a.select {|element| element.to_s.start_with?('b') } # => ["bar", :bam]
With no block given, returns a new Enumerator
.
Related: see Methods for Fetching.
With a block given, calls the block 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.
With no block given, returns a new Enumerator
.
Related: see Methods for Deleting.
Returns a new array that is self
as a transposed matrix:
a = [[:a0, :a1], [:b0, :b1], [:c0, :c1]] a.transpose # => [[:a0, :b0, :c0], [:a1, :b1, :c1]]
The elements of self
must all be the same size.
Related: see Methods for Converting.
Removes all elements from self
; returns self
:
a = [:foo, 'bar', 2] a.clear # => []
Related: see Methods for Deleting.
Returns a new array containing only the non-nil
elements from self
; element order is preserved:
a = [nil, 0, nil, false, nil, '', nil, [], nil, {}] a.compact # => [0, false, "", [], {}]
Related: Array#compact!
; see also Methods for Deleting.
Removes all nil
elements from self
; Returns self
if any elements are removed, nil
otherwise:
a = [nil, 0, nil, false, nil, '', nil, [], nil, {}] a.compact! # => [0, false, "", [], {}] a # => [0, false, "", [], {}] a.compact! # => nil
Related: Array#compact
; see also Methods for Deleting.
Returns a new array containing all but the first count
element of self
, where count
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] a.drop(9) # => []
Related: see Methods for Fetching.
Returns true
if no element of self
meets a given criterion, false
otherwise.
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 argument object
given, returns false
if for any element element
, object === element
; true
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
With a block given, 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
Related: see Methods for Querying.
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, 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
With argument object
given, returns true
if for exactly one element element
, object === 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: see Methods for Querying.
Returns elements from self
, or nil
; does not modify self
.
With no argument given, returns the first element (if available):
a = [:foo, 'bar', 2] a.first # => :foo a # => [:foo, "bar", 2]
If self
is empty, returns nil
.
[].first # => nil
With a non-negative integer argument count
given, returns the first count
elements (as available) in a new array:
a.first(0) # => [] a.first(2) # => [:foo, "bar"] a.first(50) # => [:foo, "bar", 2]
Related: see Methods for Querying.
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)