Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Complex#to_i

() -> ::Integer
to_i → integer

Returns the value of self.real as an Integer, if possible:

Example
Complex.rect(1, 0).to_i              # => 1
Complex.rect(1, Rational(0, 1)).to_i # => 1

Raises RangeError if self.imag is not exactly zero (either Integer(0) or Rational(0, n)).

See also #to_f #to_r #to_c #to_s also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0