instance method
Rational#+
(::BigDecimal) -> ::BigDecimal
(::Float) -> ::Float
(::Complex) -> ::Complex
(::Numeric) -> ::Rational
self + other → numeric
rational.c
View on GitHub ↗
VALUE
rb_rational_plus(VALUE self, VALUE other)
{
if (RB_INTEGER_TYPE_P(other)) {
{
get_dat1(self);
return f_rational_new_no_reduce2(CLASS_OF(self),
rb_int_plus(dat->num, rb_int_mul(other, dat->den)),
dat->den);
}
}
else if (RB_FLOAT_TYPE_P(other)) {
return DBL2NUM(nurat_to_double(self) + RFLOAT_VALUE(other));
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
{
get_dat2(self, other);
return f_addsub(self,
adat->num, adat->den,
bdat->num, bdat->den, '+');
}
}
else {
return rb_num_coerce_bin(self, other, '+');
}
}
Returns the sum of self and other:
Example
Rational(2, 3) + 0 # => (2/3) Rational(2, 3) + 1 # => (5/3) Rational(2, 3) + -1 # => (-1/3) Rational(2, 3) + Complex(1, 0) # => ((5/3)+0i) Rational(2, 3) + Rational(1, 1) # => (5/3) Rational(2, 3) + Rational(3, 2) # => (13/6) Rational(2, 3) + Rational(3.0, 2.0) # => (13/6) Rational(2, 3) + Rational(3.1, 2.1) # => (30399297484750849/14186338826217063)
For a computation involving Floats, the result may be inexact (see Float#+):
Example
Rational(2, 3) + 1.0 # => 1.6666666666666665 Rational(2, 3) + Complex(1.0, 0.0) # => (1.6666666666666665+0.0i)