Difference between “and” and && in Ruby?

前端 未结 7 1348
别跟我提以往
别跟我提以往 2020-11-22 09:28

What is the difference between the && and and operators in Ruby?

7条回答
  •  时光说笑
    2020-11-22 09:39

    and has lower precedence, mostly we use it as a control-flow modifier such as if:

    next if widget = widgets.pop
    

    becomes

    widget = widgets.pop and next
    

    For or:

    raise "Not ready!" unless ready_to_rock?
    

    becomes

    ready_to_rock? or raise "Not ready!"
    

    I prefer to use if but not and, because if is more intelligible, so I just ignore and and or.

    Refer to "Using “and” and “or” in Ruby" for more information.

提交回复
热议问题