Replaces the contents of self
with the contents of other_string
:
s = 'foo' # => "foo" s.replace('bar') # => "bar"
Returns the result of interpreting leading characters in self
as an integer in the given base
(which must be in (0, 2..36)):
'123456'.to_i # => 123456 '123def'.to_i(16) # => 1195503
With base
zero, string object
may contain leading characters to specify the actual base:
'123def'.to_i(0) # => 123 '0123def'.to_i(0) # => 83 '0b123def'.to_i(0) # => 1 '0o123def'.to_i(0) # => 83 '0d123def'.to_i(0) # => 123 '0x123def'.to_i(0) # => 1195503
Characters past a leading valid number (in the given base
) are ignored:
'12.345'.to_i # => 12 '12345'.to_i(2) # => 1
Returns zero if there is no leading valid number:
'abcdef'.to_i # => 0 '2'.to_i(2) # => 0
Returns the result of interpreting leading characters in self
as a Float:
'3.14159'.to_f # => 3.14159 '1.234e-2'.to_f # => 0.01234
Characters past a leading valid number (in the given base
) are ignored:
'3.14 (pi to two places)'.to_f # => 3.14
Returns zero if there is no leading valid number:
'abcdef'.to_f # => 0.0
Returns self
if self
is a String, or self
converted to a String if self
is a subclass of String.
String#to_str
is an alias for String#to_s
.
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"
Prepends each string in other_strings
to self
and returns self
:
s = 'foo' s.prepend('bar', 'baz') # => "barbazfoo" s # => "barbazfoo"
Related: String#concat
.
Returns the integer ordinal of the first character of self
:
'h'.ord # => 104 'hello'.ord # => 104 'тест'.ord # => 1090 'こんにちは'.ord # => 12371
Returns a left-justified 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
, left justified and padded on the right with pad_string
:
'hello'.ljust(10) # => "hello " ' hello'.ljust(10) # => " hello " 'hello'.ljust(10, 'ab') # => "helloababa" 'тест'.ljust(10) # => "тест " 'こんにちは'.ljust(10) # => "こんにちは "
If size
is not greater than the size of self
, returns a copy of self
:
'hello'.ljust(5) # => "hello" 'hello'.ljust(1) # => "hello"
Related: String#rjust
, String#center
.
Returns a right-justified 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
, right justified and padded on the left with pad_string
:
'hello'.rjust(10) # => " hello" 'hello '.rjust(10) # => " hello " 'hello'.rjust(10, 'ab') # => "ababahello" 'тест'.rjust(10) # => " тест" 'こんにちは'.rjust(10) # => " こんにちは"
If size
is not greater than the size of self
, returns a copy of self
:
'hello'.rjust(5, 'ab') # => "hello" 'hello'.rjust(1, 'ab') # => "hello"
Related: String#ljust
, String#center
.
Returns a copy of the receiver with leading and trailing whitespace removed; see Whitespace in Strings:
whitespace = "\x00\t\n\v\f\r " s = whitespace + 'abc' + whitespace s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r " s.strip # => "abc"
Related: String#lstrip
, String#rstrip
.
Returns a copy of self
with leading whitespace removed; see Whitespace in Strings:
whitespace = "\x00\t\n\v\f\r " s = whitespace + 'abc' + whitespace s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r " s.lstrip # => "abc\u0000\t\n\v\f\r "
Related: String#rstrip
, String#strip
.
Returns a copy of the receiver with trailing whitespace removed; see Whitespace in Strings:
whitespace = "\x00\t\n\v\f\r " s = whitespace + 'abc' + whitespace s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r " s.rstrip # => "\u0000\t\n\v\f\r abc"
Related: String#lstrip
, String#strip
.
Like String#strip
, except that any modifications are made in self
; returns self
if any modification are made, nil
otherwise.
Related: String#lstrip!
, String#strip!
.
Like String#lstrip
, except that any modifications are made in self
; returns self
if any modification are made, nil
otherwise.
Related: String#rstrip!
, String#strip!
.
Like String#rstrip
, except that any modifications are made in self
; returns self
if any modification are made, nil
otherwise.
Related: String#lstrip!
, String#strip!
.
Returns the value of float
as a BigDecimal
. The precision
parameter is used to determine the number of significant digits for the result. When precision
is set to 0
, the number of digits to represent the float being converted is determined automatically. The default precision
is 0
.
require 'bigdecimal' require 'bigdecimal/util' 0.5.to_d # => 0.5e0 1.234.to_d # => 0.1234e1 1.234.to_d(2) # => 0.12e1
See also BigDecimal::new
.
Returns a string containing a representation of self
; depending of the value of self
, the string representation may contain:
A fixed-point number.
A number in “scientific notation” (containing an exponent).
‘Infinity’.
‘-Infinity’.
‘NaN’ (indicating not-a-number).
3.14.to_s # => “3.14” (10.1**50).to_s # => “1.644631821843879e+50” (10.1**500).to_s # => “Infinity” (-10.1**500).to_s # => “-Infinity” (0.0/0.0).to_s # => “NaN”
Returns self
truncated to an Integer
.
1.2.to_i # => 1 (-1.2).to_i # => -1
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).to_i # => 2 (!)
Float#to_int
is an alias for Float#to_i
.
Returns the largest number less than or equal to self
with a precision of ndigits
decimal digits.
When 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 = -12345.6789 f.floor(1) # => -12345.7 f.floor(3) # => -12345.679
When ndigits
is non-positive, returns an integer with at least ndigits.abs
trailing zeros:
f = 12345.6789 f.floor(0) # => 12345 f.floor(-3) # => 12000 f = -12345.6789 f.floor(0) # => -12346 f.floor(-3) # => -13000
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).floor #=> 2 (!)
Related: Float#ceil
.
Since float
is already a Float
, returns self
.
Returns the value as a rational.
2.0.to_r #=> (2/1) 2.5.to_r #=> (5/2) -0.75.to_r #=> (-3/4) 0.0.to_r #=> (0/1) 0.3.to_r #=> (5404319552844595/18014398509481984)
NOTE: 0.3.to_r isn’t the same as “0.3”.to_r. The latter is equivalent to “3/10”.to_r, but the former isn’t so.
0.3.to_r == 3/10r #=> false "0.3".to_r == 3/10r #=> true
See also Float#rationalize
.
Returns the current fiber. If you are not running in the context of a fiber this method will return the root fiber.
Resumes the fiber from the point at which the last Fiber.yield
was called, or starts running it if it is the first call to resume
. Arguments passed to resume will be the value of the Fiber.yield
expression or will be passed as block parameters to the fiber’s block if this is the first resume
.
Alternatively, when resume is called it evaluates to the arguments passed to the next Fiber.yield
statement inside the fiber’s block or to the block value if it runs to completion without any Fiber.yield