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

前端 未结 23 2883
情书的邮戳
情书的邮戳 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:43

    This is the default assignment notation

    for example: x ||= 1
    this will check to see if x is nil or not. If x is indeed nil it will then assign it that new value (1 in our example)

    more explicit:
    if x == nil
    x = 1
    end

提交回复
热议问题