What does ||= (or-equals) mean in Ruby?

前端 未结 23 2952
情书的邮戳
情书的邮戳 2020-11-21 23:20

What does the following code mean in Ruby?

||=

Does it have any meaning or reason for the syntax?

23条回答
  •  青春惊慌失措
    2020-11-21 23:22

    a ||= b

    Signifies if any value is present in 'a' and you dont want to alter it the keep using that value, else if 'a' doesnt have any value, use value of 'b'.

    Simple words, if left hand side if not null, point to existing value, else point to value at right side.

提交回复
热议问题