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 in an ISO 8601 format. (This method doesn’t use the expanded representations.)
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 time as an integer number of seconds since the Epoch.
If time contains subsecond, they are truncated.
t = Time.now #=> 2020-07-21 01:41:29.746012609 +0900 t.to_i #=> 1595263289
Returns the value of time as a floating point number of seconds since the Epoch. The return value approximate the exact value in the Time
object because floating point numbers cannot represent all rational numbers exactly.
t = Time.now #=> 2020-07-20 22:00:29.38740268 +0900 t.to_f #=> 1595250029.3874028 t.to_i #=> 1595250029
Note that IEEE 754 double is not accurate enough to represent the exact number of nanoseconds since the Epoch. (IEEE 754 double has 53bit mantissa. So it can represent exact number of nanoseconds only in ‘2 ** 53 / 1_000_000_000 / 60 / 60 / 24 = 104.2` days.) When Ruby uses a nanosecond-resolution clock function, such as clock_gettime
of POSIX, to obtain the current time, Time#to_f
can lost information of a Time
object created with Time.now
.
Returns the value of time as a rational number of seconds since the Epoch.
t = Time.now #=> 2020-07-20 22:03:45.212167333 +0900 t.to_r #=> (1595250225212167333/1000000000)
This method is intended to be used to get an accurate value representing the seconds (including subsecond) since the Epoch.
Returns a string representing time. Equivalent to calling strftime
with the appropriate format string.
t = Time.now t.to_s #=> "2012-11-10 18:16:12 +0100" t.strftime "%Y-%m-%d %H:%M:%S %z" #=> "2012-11-10 18:16:12 +0100" t.utc.to_s #=> "2012-11-10 17:16:12 UTC" t.strftime "%Y-%m-%d %H:%M:%S UTC" #=> "2012-11-10 17:16:12 UTC"
Returns a ten-element array of values for time:
[sec, min, hour, day, month, year, wday, yday, isdst, zone]
See the individual methods for an explanation of the valid ranges of each value. The ten elements can be passed directly to Time.utc
or Time.local
to create a new Time
object.
t = Time.now #=> 2007-11-19 08:36:01 -0600 now = t.to_a #=> [1, 36, 8, 19, 11, 2007, 1, 323, false, "CST"]
Converts the contents of the database to an array of [key, value] arrays, and returns it.
Returns the values for this struct as an Array
.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.to_a[1] #=> "123 Maple, Anytown NC"
Returns a Hash
containing the names and values for the struct’s members.
If a block is given, the results of the block on each pair of the receiver will be used as pairs.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.to_h[:address] #=> "123 Maple, Anytown NC" joe.to_h{|name, value| [name.upcase, value.to_s.upcase]}[:ADDRESS] #=> "123 MAPLE, ANYTOWN NC"
Returns a description of this struct as a string.
Returns an integer representing the numeric file descriptor for ios.
$stdin.fileno #=> 0 $stdout.fileno #=> 1
Returns an array of all key-value pairs contained in the database.
Converts the OpenStruct
to a hash with keys representing each attribute (as symbols) and their corresponding values.
If a block is given, the results of the block on each pair of the receiver will be used as pairs.
require "ostruct" data = OpenStruct.new("country" => "Australia", :capital => "Canberra") data.to_h # => {:country => "Australia", :capital => "Canberra" } data.to_h {|name, value| [name.to_s, value.upcase] } # => {"country" => "AUSTRALIA", "capital" => "CANBERRA" }
Returns an array containing the items in the range.
(1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7] (1..).to_a #=> RangeError: cannot convert endless range to an array
Convert this range object to a printable form (using to_s
to convert the begin and end objects).
Returns a string containing the regular expression and its options (using the (?opts:source)
notation. This string can be fed back in to Regexp::new
to a regular expression with the same semantics as the original. (However, Regexp#==
may not return true when comparing the two, as the source of the regular expression itself may differ, as the example shows). Regexp#inspect
produces a generally more readable version of rxp.
r1 = /ab+c/ix #=> /ab+c/ix s1 = r1.to_s #=> "(?ix-m:ab+c)" r2 = Regexp.new(s1) #=> /(?ix-m:ab+c)/ r1 == r2 #=> false r1.source #=> "ab+c" r2.source #=> "(?ix-m:ab+c)"
Converts the set to an array. The order of elements is uncertain.
Set[1, 2].to_a #=> [1, 2] Set[1, 'c', :s].to_a #=> [1, "c", :s]