In Ruby, how do I find out if a string is not in an array?

前端 未结 5 1806
北荒
北荒 2021-01-01 08:44

In Ruby, how would I return true if a string is not in an array of options?

# pseudo code
do_this if current_subdomain not_in [\"www\", \"blog\", \"foo\", \"         


        
5条回答
  •  囚心锁ツ
    2021-01-01 09:34

    do this if not ["www", "blog", "foo", "bar"].include?(current_subdomain)
    

    or you can use grep

    >> d=["www", "blog", "foo", "bar"]
    >> d.grep(/^foo$/)
    => ["foo"]
    >> d.grep(/abc/)
    => []
    

提交回复
热议问题