Skip to content

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

instance method

Complex#to_i

cmp.to_i → integer

Returns the value as an integer if possible (the imaginary part should be exactly zero).

Example
Complex(1, 0).to_i    #=> 1
Complex(1, 0.0).to_i  # RangeError
Complex(1, 2).to_i    # RangeError
See also #to_f #to_json #to_d #to_r also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1