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

后端 未结 9 1536
再見小時候
再見小時候 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 07:56

    The simple way to get a non verbose solution is

    if ["a", "b", "c"].include? x
    

    This actually has nothing to do with ||, but rather what values are considered to be true in ruby. Everything save false and nil is true.

提交回复
热议问题