Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Numeric#to_int
() -> ::Integer
to_int → integer
numeric.c
View on GitHub ↗
static VALUE
num_to_int(VALUE num)
{
return num_funcall0(num, id_to_i);
}
Returns self as an integer; converts using method to_i in the subclass of Numeric. (Numeric itself does not define to_i.)
Of the Core and Standard Library classes, only Rational and Complex use this implementation.
Examples:
Example
Rational(1, 2).to_int # => 0 Rational(2, 1).to_int # => 2 Complex(2, 0).to_int # => 2 Complex(2, 1).to_int # Raises RangeError (non-zero imaginary part)