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!>
When invoked with a block, yield all permutations of elements of self
; returns self
. The order of permutations is indeterminate.
When a block and an in-range positive Integer
argument n
(0 < n <= self.size
) are given, calls the block with all n
-tuple permutations of self
.
Example:
a = [0, 1, 2] a.permutation(2) {|permutation| p permutation }
Output:
[0, 1] [0, 2] [1, 0] [1, 2] [2, 0] [2, 1]
Another example:
a = [0, 1, 2] a.permutation(3) {|permutation| p permutation }
Output:
[0, 1, 2] [0, 2, 1] [1, 0, 2] [1, 2, 0] [2, 0, 1] [2, 1, 0]
When n
is zero, calls the block once with a new empty Array
:
a = [0, 1, 2] a.permutation(0) {|permutation| p permutation }
Output:
[]
When n
is out of range (negative or larger than self.size
), does not call the block:
a = [0, 1, 2] a.permutation(-1) {|permutation| fail 'Cannot happen' } a.permutation(4) {|permutation| fail 'Cannot happen' }
When a block given but no argument, behaves the same as a.permutation(a.size)
:
a = [0, 1, 2] a.permutation {|permutation| p permutation }
Output:
[0, 1, 2] [0, 2, 1] [1, 0, 2] [1, 2, 0] [2, 0, 1] [2, 1, 0]
Returns a new Enumerator
if no block given:
a = [0, 1, 2] a.permutation # => #<Enumerator: [0, 1, 2]:permutation> a.permutation(2) # => #<Enumerator: [0, 1, 2]:permutation(2)>
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]
Returns the largest number less than or equal to self
with a precision of ndigits
decimal digits.
When ndigits
is negative, the returned value has at least ndigits.abs
trailing zeros:
555.floor(-1) # => 550 555.floor(-2) # => 500 -555.floor(-2) # => -600 555.floor(-3) # => 0
Returns self
when ndigits
is zero or positive.
555.floor # => 555 555.floor(50) # => 555
Related: Integer#ceil
.
Returns self
modulo other
as a real number.
For integer n
and real number r
, these expressions are equivalent:
n % r n-r*(n/r).floor n.divmod(r)[1]
See Numeric#divmod
.
Examples:
10 % 2 # => 0 10 % 3 # => 1 10 % 4 # => 2 10 % -2 # => 0 10 % -3 # => -2 10 % -4 # => -2 10 % 3.0 # => 1.0 10 % Rational(3, 1) # => (1/1)
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)
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 the Complex object created from the numerators of the real and imaginary parts of self
, after converting each part to the lowest common denominator of the two:
c = Complex.rect(Rational(2, 3), Rational(3, 4)) # => ((2/3)+(3/4)*i) c.numerator # => (8+9i)
In this example, the lowest common denominator of the two parts is 12; the two converted parts may be thought of as Rational(8, 12) and Rational(9, 12), whose numerators, respectively, are 8 and 9; so the returned value of c.numerator
is Complex.rect(8, 9)
.
Related: Complex#denominator
.
Returns a 2-element array containing two numeric elements, formed from the two operands self
and other
, of a common compatible type.
Of the Core and Standard Library classes, Integer
, Rational
, and Complex
use this implementation.
Examples:
i = 2 # => 2 i.coerce(3) # => [3, 2] i.coerce(3.0) # => [3.0, 2.0] i.coerce(Rational(1, 2)) # => [0.5, 2.0] i.coerce(Complex(3, 4)) # Raises RangeError. r = Rational(5, 2) # => (5/2) r.coerce(2) # => [(2/1), (5/2)] r.coerce(2.0) # => [2.0, 2.5] r.coerce(Rational(2, 3)) # => [(2/3), (5/2)] r.coerce(Complex(3, 4)) # => [(3+4i), ((5/2)+0i)] c = Complex(2, 3) # => (2+3i) c.coerce(2) # => [(2+0i), (2+3i)] c.coerce(2.0) # => [(2.0+0i), (2+3i)] c.coerce(Rational(1, 2)) # => [((1/2)+0i), (2+3i)] c.coerce(Complex(3, 4)) # => [(3+4i), (2+3i)]
Raises an exception if any type conversion fails.
Returns self
.
Raises an exception if the value for freeze
is neither true
nor nil
.
Related: Numeric#dup
.
Returns self
modulo other
as a real number.
Of the Core and Standard Library classes, only Rational
uses this implementation.
For Rational
r
and real number n
, these expressions are equivalent:
r % n r-n*(r/n).floor r.divmod(n)[1]
See Numeric#divmod
.
Examples:
r = Rational(1, 2) # => (1/2) r2 = Rational(2, 3) # => (2/3) r % r2 # => (1/2) r % 2 # => (1/2) r % 2.0 # => 0.5 r = Rational(301,100) # => (301/100) r2 = Rational(7,5) # => (7/5) r % r2 # => (21/100) r % -r2 # => (-119/100) (-r) % r2 # => (119/100) (-r) %-r2 # => (-21/100)
Returns the remainder after dividing self
by other
.
Of the Core and Standard Library classes, only Float
and Rational
use this implementation.
Examples:
11.0.remainder(4) # => 3.0 11.0.remainder(-4) # => 3.0 -11.0.remainder(4) # => -3.0 -11.0.remainder(-4) # => -3.0 12.0.remainder(4) # => 0.0 12.0.remainder(-4) # => 0.0 -12.0.remainder(4) # => -0.0 -12.0.remainder(-4) # => -0.0 13.0.remainder(4.0) # => 1.0 13.0.remainder(Rational(4, 1)) # => 1.0 Rational(13, 1).remainder(4) # => (1/1) Rational(13, 1).remainder(-4) # => (1/1) Rational(-13, 1).remainder(4) # => (-1/1) Rational(-13, 1).remainder(-4) # => (-1/1)
Returns true
if zero
has a zero value, false
otherwise.
Of the Core and Standard Library classes, only Rational
and Complex
use this implementation.
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 the largest number that is less than or equal to self
with a precision of digits
decimal digits.
Numeric implements this by converting self
to a Float
and invoking Float#floor
.
Returns the numerator.
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 the Integer
byte-based index of the last occurrence of the given substring
, or nil
if none found:
'foo'.byterindex('f') # => 0 'foo'.byterindex('o') # => 2 'foo'.byterindex('oo') # => 1 'foo'.byterindex('ooo') # => nil
Returns the Integer
byte-based index of the last match for the given Regexp
regexp
, or nil
if none found:
'foo'.byterindex(/f/) # => 0 'foo'.byterindex(/o/) # => 2 'foo'.byterindex(/oo/) # => 1 'foo'.byterindex(/ooo/) # => nil
The last match means starting at the possible last position, not the last of longest matches.
'foo'.byterindex(/o+/) # => 2 $~ #=> #<MatchData "o">
To get the last longest match, needs to combine with negative lookbehind.
'foo'.byterindex(/(?<!o)o+/) # => 1 $~ #=> #<MatchData "oo">
Or String#byteindex
with negative lookforward.
'foo'.byteindex(/o+(?!.*o)/) # => 1 $~ #=> #<MatchData "oo">
Integer
argument offset
, if given and non-negative, specifies the maximum starting byte-based position in the
string to _end_ the search: 'foo'.byterindex('o', 0) # => nil 'foo'.byterindex('o', 1) # => 1 'foo'.byterindex('o', 2) # => 2 'foo'.byterindex('o', 3) # => 2
If offset
is a negative Integer
, the maximum starting position in the string to end the search is the sum of the string’s length and offset
:
'foo'.byterindex('o', -1) # => 2 'foo'.byterindex('o', -2) # => 1 'foo'.byterindex('o', -3) # => nil 'foo'.byterindex('o', -4) # => nil
If offset
does not land on character (codepoint) boundary, IndexError
is raised.
Related: String#byteindex
.
Returns the byte at zero-based index
as an integer, or nil
if index
is out of range:
s = 'abcde' # => "abcde" s.getbyte(0) # => 97 s.getbyte(-1) # => 101 s.getbyte(5) # => nil
Related: String#setbyte
.
Returns a new string with the characters from self
in reverse order.
'stressed'.reverse # => "desserts"
Returns self
with its characters reversed:
s = 'stressed' s.reverse! # => "desserts" s # => "desserts"
Returns the Symbol
corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name
.
"Koala".intern #=> :Koala s = 'cat'.to_sym #=> :cat s == :cat #=> true s = '@cat'.to_sym #=> :@cat s == :@cat #=> true
This can also be used to create symbols that cannot be represented using the :xxx
notation.
'cat and dog'.to_sym #=> :"cat and dog"
Returns a centered copy of self
.
If integer argument size
is greater than the size (in characters) of self
, returns a new string of length size
that is a copy of self
, centered and padded on both ends with pad_string
:
'hello'.center(10) # => " hello " ' hello'.center(10) # => " hello " 'hello'.center(10, 'ab') # => "abhelloaba" 'тест'.center(10) # => " тест " 'こんにちは'.center(10) # => " こんにちは "
If size
is not greater than the size of self
, returns a copy of self
:
'hello'.center(5) # => "hello" 'hello'.center(1) # => "hello"
Related: String#ljust
, String#rjust
.