Skip to content

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

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)
See also #to_c #truncate #step #zero? also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0