Returns exception’s message (or the name of the exception if no message is set).
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.
Converts the value to a string.
The default format looks like 0.xxxxEnn.
The optional parameter s consists of either an integer; or an optional ‘+’ or ‘ ’, followed by an optional number, followed by an optional ‘E’ or ‘F’.
If there is a ‘+’ at the start of s, positive values are returned with a leading ‘+’.
A space at the start of s returns positive values with a leading space.
If s contains a number, a space is inserted after each group of that many fractional digits.
If s ends with an ‘E’, engineering notation (0.xxxxEnn) is used.
If s ends with an ‘F’, conventional floating point notation is used.
Examples:
BigDecimal('-123.45678901234567890').to_s('5F') #=> '-123.45678 90123 45678 9' BigDecimal('123.45678901234567890').to_s('+8F') #=> '+123.45678901 23456789' BigDecimal('123.45678901234567890').to_s(' F') #=> ' 123.4567890123456789'
Returns the value as an Integer
.
If the BigDecimal
is infinity or NaN, raises FloatDomainError
.
Converts a BigDecimal
to a Rational
.
Returns a new Float
object having approximately the same value as the BigDecimal
number. Normal accuracy limits and built-in errors of binary Float
arithmetic apply.
Returns the value as a BigDecimal
.
The required precision
parameter is used to determine the number of significant digits for the result.
require 'bigdecimal' require 'bigdecimal/util' Rational(22, 7).to_d(3) # => 0.314e1
See also BigDecimal::new
.
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
Returns the value as a string.
Rational(2).to_s #=> "2/1" Rational(-8, 6).to_s #=> "-4/3" Rational('1/2').to_s #=> "1/2"
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
Time#tv_sec
is an alias for Time#to_i
.
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"]
Range#entries
is an alias for Range#to_a
.
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 a string showing the options and string of self
:
r0 = /ab+c/ix s0 = r0.to_s # => "(?ix-m:ab+c)"
The returned string may be used as an argument to Regexp.new
, or as interpolated text for a Regexp literal:
r1 = Regexp.new(s0) # => /(?ix-m:ab+c)/ r2 = /#{s0}/ # => /(?ix-m:ab+c)/
Note that r1
and r2
are not equal to r0
because their original strings are different:
r0 == r1 # => false r0.source # => "ab+c" r1.source # => "(?ix-m:ab+c)"
Related: Regexp#inspect
.