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

后端 未结 9 1530
再見小時候
再見小時候 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:11

    I see a lot of people preferring the include? comparison.

    I prefer to use the .in? operator. It is much more succinct. And also more readable, since we don't ask questions to the array, we ask questions to the variable you want to ask: On your case, the controller name.

    @controller.controller_name.in? ["projects", "parts"]
    

    Or, even better

    @controller.controller_name.in? %w[projects parts]
    

提交回复
热议问题