Difference between “and” and && in Ruby?

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

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

7条回答
  •  臣服心动
    2020-11-22 09:43

    and has lower precedence than &&.

    But for an unassuming user, problems might occur if it is used along with other operators whose precedence are in between, for example, the assignment operator:

    def happy?() true; end
    def know_it?() true; end
    
    todo = happy? && know_it? ? "Clap your hands" : "Do Nothing"
    
    todo
    # => "Clap your hands"
    
    todo = happy? and know_it? ? "Clap your hands" : "Do Nothing"
    
    todo
    # => true
    

提交回复
热议问题