What does "+=" (plus equals) mean? What does "+=" (plus equals) mean? ruby ruby

What does "+=" (plus equals) mean?


+= is a shorthand operator.

someVar += otherVar

is the same as

someVar = someVar + otherVar


Expressions with binary operators of the form:

x = x op y

Can be written as:

x op= y

For instance:

x += y   # x = x + yx /= y   # x = x / yx ||= y  # x = x || y (but see disclaimer)

However, be warned that ||= and &&= can behave slightly ... different (most evident when used in conjunction with a hash indexer). Plenty of SO questions about this oddity though.

Happy coding.


Not an ruby expert but I would think that it either appends to an existing String or increments an numeric variable?