Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Complex#+
complex + numeric → new_complex
complex.c
View on GitHub ↗
VALUE
rb_complex_plus(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
VALUE real, imag;
get_dat2(self, other);
real = f_add(adat->real, bdat->real);
imag = f_add(adat->imag, bdat->imag);
return f_complex_new2(CLASS_OF(self), real, imag);
}
if (k_numeric_p(other) && f_real_p(other)) {
get_dat1(self);
return f_complex_new2(CLASS_OF(self),
f_add(dat->real, other), dat->imag);
}
return rb_num_coerce_bin(self, other, '+');
}
Returns the sum of self and numeric:
Example
Complex(2, 3) + Complex(2, 3) # => (4+6i) Complex(900) + Complex(1) # => (901+0i) Complex(-2, 9) + Complex(-9, 2) # => (-11+11i) Complex(9, 8) + 4 # => (13+8i) Complex(20, 9) + 9.8 # => (29.8+9i)