Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Numeric#to_int

to_int → integer

Returns self as an integer; converts using method to_i in the derived class.

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)         # Raises RangeError (non-zero imaginary part)
See also #to_c #truncate #step #zero? also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0