Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
Complex
A Complex object houses a pair of values, given when the object is created as either rectangular coordinates or polar coordinates.
Rectangular Coordinates
The rectangular coordinates of a complex number are called the real and imaginary parts; see Complex number definition.
You can create a Complex object from rectangular coordinates with:
-
A complex literal.
-
Method
Complex.rect. -
Method
Kernel#Complex, either with numeric arguments or with certain string arguments. -
Method
String#to_c, for certain strings.
Note that each of the stored parts may be a an instance one of the classes Complex, Float, Integer, or Rational; they may be retrieved:
-
Separately, with methods
Complex#realandComplex#imaginary. -
Together, with method
Complex#rect.
The corresponding (computed) polar values may be retrieved:
-
Separately, with methods
Complex#absandComplex#arg. -
Together, with method
Complex#polar.
Polar Coordinates
The polar coordinates of a complex number are called the absolute and argument parts; see Complex polar plane.
In this class, the argument part in expressed radians (not degrees).
You can create a Complex object from polar coordinates with:
-
Method
Complex.polar. -
Method
Kernel#Complex, with certain string arguments. -
Method
String#to_c, for certain strings.
Note that each of the stored parts may be a an instance one of the classes Complex, Float, Integer, or Rational; they may be retrieved:
-
Separately, with methods
Complex#absandComplex#arg. -
Together, with method
Complex#polar.
The corresponding (computed) rectangular values may be retrieved:
-
Separately, with methods
Complex#realandComplex#imag. -
Together, with method
Complex#rect.
Class methods 4
Instance methods 40
Equivalent to Complex(0, 1):
Complex::I # => (0+1i)