Returns a new Array
whose elements are all those from self
for which the block returns false
or nil
:
a = [:foo, 'bar', 2, 'bat'] a1 = a.reject {|element| element.to_s.start_with?('b') } a1 # => [:foo, 2]
Returns a new Enumerator
if no block given:
a = [:foo, 'bar', 2] a.reject # => #<Enumerator: [:foo, "bar", 2]:reject>
Removes each element for which the block returns a truthy value.
Returns self
if any elements removed:
a = [:foo, 'bar', 2, 'bat'] a.reject! {|element| element.to_s.start_with?('b') } # => [:foo, 2]
Returns nil
if no elements removed.
Returns a new Enumerator
if no block given:
a = [:foo, 'bar', 2] a.reject! # => #<Enumerator: [:foo, "bar", 2]:reject!>
Returns true
if for some index i
in self
, obj == self[i]
; otherwise false
:
[0, 1, 2].include?(2) # => true [0, 1, 2].include?(3) # => false
Returns a new Array
containing those elements from self
that are not duplicates, the first occurrence always being retained.
With no block given, identifies and omits duplicates using method eql?
to compare:
a = [0, 0, 1, 1, 2, 2] a.uniq # => [0, 1, 2]
With a block given, calls the block for each element; identifies (using method eql?
) and omits duplicate values, that is, those elements for which the block returns the same value:
a = ['a', 'aa', 'aaa', 'b', 'bb', 'bbb'] a.uniq {|element| element.size } # => ["a", "aa", "aaa"]
Removes duplicate elements from self
, the first occurrence always being retained; returns self
if any elements removed, nil
otherwise.
With no block given, identifies and removes elements using method eql?
to compare.
Returns self
if any elements removed:
a = [0, 0, 1, 1, 2, 2] a.uniq! # => [0, 1, 2]
Returns nil
if no elements removed.
With a block given, calls the block for each element; identifies (using method eql?
) and removes elements for which the block returns duplicate values.
Returns self
if any elements removed:
a = ['a', 'aa', 'aaa', 'b', 'bb', 'bbb'] a.uniq! {|element| element.size } # => ['a', 'aa', 'aaa']
Returns nil
if no elements removed.
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 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
.
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 self
rounded to the nearest value with a precision of ndigits
decimal digits.
When ndigits
is negative, the returned value has at least ndigits.abs
trailing zeros:
555.round(-1) # => 560 555.round(-2) # => 600 555.round(-3) # => 1000 -555.round(-2) # => -600 555.round(-4) # => 0
Returns self
when ndigits
is zero or positive.
555.round # => 555 555.round(1) # => 555 555.round(50) # => 555
If keyword argument half
is given, and self
is equidistant from the two candidate values, the rounding is according to the given half
value:
:up
or nil
: round away from zero:
25.round(-1, half: :up) # => 30 (-25).round(-1, half: :up) # => -30
:down
: round toward zero:
25.round(-1, half: :down) # => 20 (-25).round(-1, half: :down) # => -20
:even
: round toward the candidate whose last nonzero digit is even:
25.round(-1, half: :even) # => 20 15.round(-1, half: :even) # => 20 (-25).round(-1, half: :even) # => -20
Raises and exception if the value for half
is invalid.
Related: Integer#truncate
.
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.
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 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
; 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 the conjugate of self
, Complex.rect(self.imag, self.real)
:
Complex.rect(1, 2).conj # => (1-2i)
Returns the conjugate of self
, Complex.rect(self.imag, self.real)
:
Complex.rect(1, 2).conj # => (1-2i)
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 array [self, 0]
.
Returns self
.
Raises an exception if the value for freeze
is neither true
nor nil
.
Related: Numeric#dup
.
Returns +self+ if +self+ is not a zero value, +nil+ otherwise; uses method <tt>zero?</tt> for the evaluation. The returned +self+ allows the method to be chained: a = %w[z Bb bB bb BB a aA Aa AA A] a.sort {|a, b| (a.downcase <=> b.downcase).nonzero? || a <=> b } # => ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"] Of the Core and Standard Library classes, Integer, Float, Rational, and Complex use this implementation.
Related: zero?
Returns self
rounded to the nearest value with a precision of digits
decimal digits.
Numeric implements this by converting self
to a Float
and invoking Float#round
.
Returns true
if self
is greater than 0, false
otherwise.
Returns true
if self
is less than 0, false
otherwise.