Understanding the “||” OR operator in If conditionals in Ruby

后端 未结 9 1524
再見小時候
再見小時候 2020-11-28 07:40

Just briefly, why are the following three lines not identical in their impact?

if @controller.controller_name == \"projects\" || @controller.controller_name          


        
9条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 08:00

    The difference is the order of what's happening. Also the || isn't doing what you think it does in the 2 and 3.

    You can also do

    if ['projects','parts'].include?(@controller.controller_name)
    

    to reduce code in the future if you need to add more matches.

提交回复
热议问题