Test if variable matches any of several strings w/o long if-elsif chain, or case-when

后端 未结 3 1582
忘掉有多难
忘掉有多难 2020-12-03 21:08

I assume there is a nice one-line way to say in ruby

if mystr == \"abc\" or \"def \" or \"ghi\" or \"xyz\"

but cannot find how to do that i

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 21:53

    You can use Array#include? like this:

    if ["abc", "def ", "ghi", "xyz"].include?(mystr)
    

提交回复
热议问题