Ruby 元素是否存在于数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3482125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:02:53  来源:igfitidea点击:

Ruby element present in array or not

ruby-on-railsruby

提问by Sachin R

Possible Duplicate:
how to check if my array includes an object - rails

可能的重复:
如何检查我的数组是否包含一个对象 - rails

I have array

我有数组

array['one', 'two', 'three']

How i find that 'two' element present in array.

我如何找到数组中存在的“两个”元素。

Is any method in ruby which can find this?

ruby 中有什么方法可以找到这个吗?

Thanks

谢谢

回答by zetetic

array.include?('two')returns trueor false

array.include?('two')返回truefalse

回答by Ankit Bansal

you can use:

您可以使用:

array.index('two')

will return index of object if present else will return nil.

如果存在,将返回对象的索引,否则将返回 nil。