Kernel#Complex
static VALUE
nucomp_f_complex(int argc, VALUE *argv, VALUE klass)
{
VALUE a1, a2, opts = Qnil;
int raise = TRUE;
if (rb_scan_args(argc, argv, "11:", &a1, &a2, &opts) == 1) {
a2 = Qundef;
}
if (!NIL_P(opts)) {
raise = rb_opts_exception_p(opts, raise);
}
if (argc > 0 && CLASS_OF(a1) == rb_cComplex && UNDEF_P(a2)) {
return a1;
}
return nucomp_convert(rb_cComplex, a1, a2, raise);
}
Returns a new Complex object if the arguments are valid; otherwise raises an exception if exception is true; otherwise returns nil.
With Numeric arguments real and imag, returns Complex.rect(real, imag) if the arguments are valid.
With string argument s, returns a new Complex object if the argument is valid; the string may have:
-
One or two numeric substrings, each of which specifies a
Complex,Float,Integer,Numeric, orRationalvalue, specifying rectangular coordinates:-
Sign-separated real and imaginary numeric substrings (with trailing character
'i'):ExampleComplex('1+2i') # => (1+2i) Complex('+1+2i') # => (1+2i) Complex('+1-2i') # => (1-2i) Complex('-1+2i') # => (-1+2i) Complex('-1-2i') # => (-1-2i)
-
Real-only numeric string (without trailing character
'i'):ExampleComplex('1') # => (1+0i) Complex('+1') # => (1+0i) Complex('-1') # => (-1+0i)
-
Imaginary-only numeric string (with trailing character
'i'):ExampleComplex('1i') # => (0+1i) Complex('+1i') # => (0+1i) Complex('-1i') # => (0-1i)
-
-
At-sign separated real and imaginary rational substrings, each of which specifies a
Rationalvalue, specifying polar coordinates:ExampleComplex('1/2@3/4') # => (0.36584443443691045+0.34081938001166706i) Complex('+1/2@+3/4') # => (0.36584443443691045+0.34081938001166706i) Complex('+1/2@-3/4') # => (0.36584443443691045-0.34081938001166706i) Complex('-1/2@+3/4') # => (-0.36584443443691045-0.34081938001166706i) Complex('-1/2@-3/4') # => (-0.36584443443691045+0.34081938001166706i)