Returns the result of interpreting leading characters in str
as a rational. Leading whitespace and extraneous characters past the end of a valid number are ignored. Digit sequences can be separated by an underscore. If there is not a valid number at the start of str
, zero is returned. This method never raises an exception.
' 2 '.to_r #=> (2/1) '300/2'.to_r #=> (150/1) '-9.2'.to_r #=> (-46/5) '-9.2e2'.to_r #=> (-920/1) '1_234_567'.to_r #=> (1234567/1) '21 June 09'.to_r #=> (21/1) '21/06/09'.to_r #=> (7/2) 'BWV 1079'.to_r #=> (0/1)
NOTE: “0.3”.to_r isn’t the same as 0.3.to_r. The former is equivalent to “3/10”.to_r, but the latter isn’t so.
"0.3".to_r == 3/10r #=> true 0.3.to_r == 3/10r #=> false
See also Kernel#Rational
.
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 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 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 a string representing obj. The default to_s
prints the object’s class and an encoding of the object id. As a special case, the top-level object that is the initial execution context of Ruby
programs returns “main”.
Returns a string representation of self
:
x = RuntimeError.new('Boom') x.to_s # => "Boom" x = RuntimeError.new x.to_s # => "RuntimeError"
Returns a string representing this module or class. For basic classes and modules, this is the name. For singletons, we show information on the thing we’re attached to as well.
Returns a string representation of the date in self
in ISO 8601 extended date format ('%Y-%m-%d'
):
Date.new(2001, 2, 3).to_s # => "2001-02-03"
Returns a string in an ISO 8601 format. (This method doesn’t use the expanded representations.)
DateTime.new(2001,2,3,4,5,6,'-7').to_s #=> "2001-02-03T04:05:06-07:00"
Returns the value of self
as integer Epoch seconds; subseconds are truncated (not rounded):
Time.utc(1970, 1, 1, 0, 0, 0).to_i # => 0 Time.utc(1970, 1, 1, 0, 0, 0, 999999).to_i # => 0 Time.utc(1950, 1, 1, 0, 0, 0).to_i # => -631152000 Time.utc(1990, 1, 1, 0, 0, 0).to_i # => 631152000
Returns the value of self
as a Float
number Epoch seconds; subseconds are included.
The stored value of self
is a Rational, which means that the returned value may be approximate:
Time.utc(1970, 1, 1, 0, 0, 0).to_f # => 0.0 Time.utc(1970, 1, 1, 0, 0, 0, 999999).to_f # => 0.999999 Time.utc(1950, 1, 1, 0, 0, 0).to_f # => -631152000.0 Time.utc(1990, 1, 1, 0, 0, 0).to_f # => 631152000.0
Returns the value of self
as a Rational
exact number of Epoch seconds;
Time.now.to_r # => (16571402750320203/10000000)
Returns a string representation of self
, without subseconds:
t = Time.new(2000, 12, 31, 23, 59, 59, 0.5) t.to_s # => "2000-12-31 23:59:59 +0000"
Related: Time#ctime
, Time#inspect
:
t.ctime # => "Sun Dec 31 23:59:59 2000" t.inspect # => "2000-12-31 23:59:59.5 +000001"
Returns a 10-element array of values representing self
:
Time.utc(2000, 1, 1).to_a # => [0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"] # [sec, min, hour, day, mon, year, wday, yday, dst?, zone]
The returned array is suitable for use as an argument to Time.utc
or Time.local
to create a new Time
object.
Returns the integer file descriptor for the stream:
$stdin.fileno # => 0 $stdout.fileno # => 1 $stderr.fileno # => 2 File.open('t.txt').fileno # => 10 f.close
Returns an array containing the elements in self
, if a finite collection; raises an exception otherwise.
(1..4).to_a # => [1, 2, 3, 4] (1...4).to_a # => [1, 2, 3] ('a'..'d').to_a # => ["a", "b", "c", "d"]
Returns a string representation of self
, including begin.to_s
and end.to_s
:
(1..4).to_s # => "1..4" (1...4).to_s # => "1...4" (1..).to_s # => "1.." (..4).to_s # => "..4"
Note that returns from to_s
and inspect
may differ:
('a'..'d').to_s # => "a..d" ('a'..'d').inspect # => "\"a\"..\"d\""
Related: Range#inspect
.
Returns the truncated value as an integer.
Equivalent to Rational#truncate
.
Rational(2, 3).to_i #=> 0 Rational(3).to_i #=> 3 Rational(300.6).to_i #=> 300 Rational(98, 71).to_i #=> 1 Rational(-31, 2).to_i #=> -15
Returns the value as a Float
.
Rational(2).to_f #=> 2.0 Rational(9, 4).to_f #=> 2.25 Rational(-3, 4).to_f #=> -0.75 Rational(20, 3).to_f #=> 6.666666666666667