在Ruby中,我怎么找出是否一个字符串是不是在一个数组? [英] In Ruby, how do I find out if a string is not in an array?

查看:600
本文介绍了在Ruby中,我怎么找出是否一个字符串是不是在一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby中,我将如何,如果一个字符串是不是在一组选项返回true?

 #伪code
do_this如果current_subdomain NOT_IN [WWW,博客,富,酒吧]

...或者你知道一个更好的方式来写这个?


解决方案

  do_this除非[WWW,博客,富,酒吧。包括哪些内容?(current_subdomain )

  do_this如果没有[WWW,博客,富,酒吧。包括什么呢?(current_subdomain)

我使用的是阵列#包括什么呢?方法。

不过使用除非是一个相当大的红宝石成语。

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", "bar"]

...or do you know of a better way to write this?

解决方案

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.

这篇关于在Ruby中,我怎么找出是否一个字符串是不是在一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆