Difference between “and” and && in Ruby?

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

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

7条回答
  •  无人共我
    2020-11-22 09:54

    The Ruby Style Guide says it better than I could:

    Use &&/|| for boolean expressions, and/or for control flow. (Rule of thumb: If you have to use outer parentheses, you are using the wrong operators.)

    # boolean expression
    if some_condition && some_other_condition
      do_something
    end
    
    # control flow
    document.saved? or document.save!
    

提交回复
热议问题