What does “+=” (plus equals) mean?

前端 未结 4 1091
滥情空心
滥情空心 2020-12-09 01:43

I am doing some ruby exercises and it said I need to go back and rewrite the script with += shorthand notations.

This exercise deals primarily with lear

4条回答
  •  误落风尘
    2020-12-09 02:39

    Expressions with binary operators of the form:

    x = x op y
    

    Can be written as:

    x op= y
    

    For instance:

    x += y   # x = x + y
    x /= y   # x = x / y
    x ||= 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.

提交回复
热议问题