Skip to content
instance method

Complex#to_r

() -> ::Rational
to_r → rational

Returns the value of self.real as a Rational, if possible:

Example
Complex.rect(1, 0).to_r              # => (1/1)
Complex.rect(1, Rational(0, 1)).to_r # => (1/1)
Complex.rect(1, 0.0).to_r            # => (1/1)

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

Related: Complex#rationalize.

See also #to_json #to_s #to_i also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0