is there a ruby one-line “return if x”?

后端 未结 4 1378
有刺的猬
有刺的猬 2021-02-06 22:08

have a ton of places I need to add

if this_flag
  return
end

can that be done on one line with ruby?

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-06 22:59

    Some additions to Jörg W Mittag's good answer:

    x && return
    x and return
    if x then return end
    

    I do not actually recommend the first two forms: however, the above examples are all valid productions. I personally prefer to avoid return in general -- most grammar constructs in Ruby are usable expressions.

    Happy coding.

提交回复
热议问题