method

coerce

v1_9_3_125 - Show latest stable - Class: Rational
coerce(p1)
public

No documentation available.

static VALUE
nurat_coerce(VALUE self, VALUE other)
{
    switch (TYPE(other)) {
      case T_FIXNUM:
      case T_BIGNUM:
        return rb_assoc_new(f_rational_new_bang1(CLASS_OF(self), other), self);
      case T_FLOAT:
        return rb_assoc_new(other, f_to_f(self));
      case T_RATIONAL:
        return rb_assoc_new(other, self);
      case T_COMPLEX:
        if (k_exact_zero_p(RCOMPLEX(other)->imag))
            return rb_assoc_new(f_rational_new_bang1
                                (CLASS_OF(self), RCOMPLEX(other)->real), self);
        else
            return rb_assoc_new(other, rb_Complex(self, INT2FIX(0)));
    }

    rb_raise(rb_eTypeError, "%s can't be coerced into %s",
             rb_obj_classname(other), rb_obj_classname(self));
    return Qnil;
}