instance method
Kernel#Rational
(::int | ::_ToR rational_like, ?exception: true) -> ::Rational
(::int | ::_ToR rational_like, exception: bool) -> ::Rational?
(::int | ::_ToR numer, ?::int | ::_ToR denom, ?exception: true) -> ::Rational
(::int | ::_ToR numer, ?::int | ::_ToR denom, exception: bool) -> ::Rational?
[T] (::Numeric & ::Kernel::_RationalDiv[T] numer, ::Numeric denom, ?exception: bool) -> T
[T < ::Numeric] (T value, 1, ?exception: bool) -> T
(untyped, ?untyped, ?exception: bool) -> ::Rational?
Rational(x, y, exception: true) → rational or nil
Rational(arg, exception: true) → rational or nil
rational.c
View on GitHub ↗
static VALUE
nurat_f_rational(int argc, VALUE *argv, VALUE klass)
{
VALUE a1, a2, opts = Qnil;
int raise = TRUE;
if (rb_scan_args(argc, argv, "11:", &a1, &a2, &opts) == 1) {
a2 = Qundef;
}
if (!NIL_P(opts)) {
raise = rb_opts_exception_p(opts, raise);
}
return nurat_convert(rb_cRational, a1, a2, raise);
}
Returns x/y or arg as a Rational.
Example
Rational(2, 3) #=> (2/3) Rational(5) #=> (5/1) Rational(0.5) #=> (1/2) Rational(0.3) #=> (5404319552844595/18014398509481984) Rational("2/3") #=> (2/3) Rational("0.3") #=> (3/10) Rational("10 cents") #=> ArgumentError Rational(nil) #=> TypeError Rational(1, nil) #=> TypeError Rational("10 cents", exception: false) #=> nil
Syntax of the string form:
string form = extra spaces , rational , extra spaces ;
rational = [ sign ] , unsigned rational ;
unsigned rational = numerator | numerator , "/" , denominator ;
numerator = integer part | fractional part | integer part , fractional part ;
denominator = digits ;
integer part = digits ;
fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ;
sign = "-" | "+" ;
digits = digit , { digit | "_" , digit } ;
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
extra spaces = ? \s* ? ;
See also String#to_r.