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

前端 未结 5 1839
北荒
北荒 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条回答
  •  萌比男神i
    2021-01-01 09:43

    do_this unless  ["www", "blog", "foo", "bar"].include?(current_subdomain)
    

    or

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

    I'm using the Array#include? method.

    However using unless is a fairly big ruby idiom.

提交回复
热议问题