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 float or integer that is less than or equal to self
, as specified by the given ‘ndigits`, which must be an [integer-convertible object](Integer-Convertible Objects). at implicit_conversion.rdoc
Equivalent to self.to_f.floor(ndigits)
.
Related: ceil
, 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
.
Returns a 2-element array containing other
converted to a Float and self
:
f = 3.14 # => 3.14 f.coerce(2) # => [2.0, 3.14] f.coerce(2.0) # => [2.0, 3.14] f.coerce(Rational(1, 2)) # => [0.5, 3.14] f.coerce(Complex(1, 0)) # => [1.0, 3.14]
Raises an exception if a type conversion fails.
Returns self
modulo other
as a float.
For float f
and real number r
, these expressions are equivalent:
f % r f-r*(f/r).floor f.divmod(r)[1]
See Numeric#divmod
.
Examples:
10.0 % 2 # => 0.0 10.0 % 3 # => 1.0 10.0 % 4 # => 2.0 10.0 % -2 # => 0.0 10.0 % -3 # => -2.0 10.0 % -4 # => -2.0 10.0 % 4.0 # => 2.0 10.0 % Rational(4, 1) # => 2.0
Returns a float or integer that is a “floor” value for self
, as specified by ndigits
, which must be an integer-convertible object.
When self
is zero, returns a zero value: a float if ndigits
is positive, an integer otherwise:
f = 0.0 # => 0.0 f.floor(20) # => 0.0 f.floor(0) # => 0 f.floor(-20) # => 0
When self
is non-zero and ndigits
is positive, returns a float with ndigits
digits after the decimal point (as available):
f = 12345.6789 f.floor(1) # => 12345.6 f.floor(3) # => 12345.678 f.floor(30) # => 12345.6789 f = -12345.6789 f.floor(1) # => -12345.7 f.floor(3) # => -12345.679 f.floor(30) # => -12345.6789
When self
is non-zero and ndigits
is non-positive, returns an integer value based on a computed granularity:
The granularity is 10 ** ndigits.abs
.
The returned value is the largest multiple of the granularity that is less than or equal to self
.
Examples with positive self
:
ndigits | Granularity | 12345.6789.floor(ndigits) |
---|---|---|
0 | 1 | 12345 |
-1 | 10 | 12340 |
-2 | 100 | 12300 |
-3 | 1000 | 12000 |
-4 | 10000 | 10000 |
-5 | 100000 | 0 |
Examples with negative self
:
ndigits | Granularity | -12345.6789.floor(ndigits) |
---|---|---|
0 | 1 | -12346 |
-1 | 10 | -12350 |
-2 | 100 | -12400 |
-3 | 1000 | -13000 |
-4 | 10000 | -20000 |
-5 | 100000 | -100000 |
-6 | 1000000 | -1000000 |
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).floor # => 2 # Not 3, (because (0.3 / 0.1) # => 2.9999999999999996, not 3.0)
Related: Float#ceil
.
Returns true
if self
is 0.0, false
otherwise.
Returns the numerator. The result is machine dependent.
n = 0.3.numerator #=> 5404319552844595 d = 0.3.denominator #=> 18014398509481984 n.fdiv(d) #=> 0.3
See also Float#denominator
.
Forces the fiber to be blocking for the duration of the block. Returns the result of the block.
See the “Non-blocking fibers” section in class docs for details.