coerce(p1)
public
If aNumeric is the same type as
num, returns an array containing aNumeric and num. Otherwise, returns
an array with both aNumeric and
num represented as Float
objects. This coercion mechanism is used by Ruby to handle mixed-type
numeric operations: it is intended to find a compatible common type between
the two operands of the operator.
1.coerce(2.5)
1.2.coerce(3)
1.coerce(2)
Show source
/*
* call-seq:
* num.coerce(numeric) => array
*
* If <i>aNumeric</i> is the same type as <i>num</i>, returns an array
* containing <i>aNumeric</i> and <i>num</i>. Otherwise, returns an
* array with both <i>aNumeric</i> and <i>num</i> represented as
* <code>Float</code> objects. This coercion mechanism is used by
* Ruby to handle mixed-type numeric operations: it is intended to
* find a compatible common type between the two operands of the operator.
*
* 1.coerce(2.5)
* 1.2.coerce(3)
* 1.coerce(2)
*/
static VALUE
num_coerce(x, y)
VALUE x, y;
{
if (CLASS_OF(x) == CLASS_OF(y))
return rb_assoc_new(y, x);
x = rb_Float(x);
y = rb_Float(y);
return rb_assoc_new(y, x);
}