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
.
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 a string containing a representation of self
; depending of the value of self
, the string representation may contain:
A fixed-point number.
3.14.to_s # => "3.14"
A number in “scientific notation” (containing an exponent).
(10.1**50).to_s # => "1.644631821843879e+50"
‘Infinity’.
(10.1**500).to_s # => "Infinity"
‘-Infinity’.
(-10.1**500).to_s # => "-Infinity"
‘NaN’ (indicating not-a-number).
(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 (!)
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 self
(which is already a Float).
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
Returns an array of the entry names in the directory at dirpath
except for '.'
and '..'
; sets the given encoding onto each returned entry name:
Dir.children('/example') # => ["config.h", "lib", "main.rb"] Dir.children('/example').first.encoding # => #<Encoding:UTF-8> Dir.children('/example', encoding: 'US-ASCII').first.encoding # => #<Encoding:US-ASCII>
See String Encoding.
Raises an exception if the directory does not exist.