查找数组中int或字符串匹配的位置

Qwe*_*tie 0 ruby arrays search

有什么方法我不仅可以找出一个数组是否包含某些内容,还有数组中的位置?

例如

发现1[1,4,6,4,1]将返回[0,4]

Ger*_*rry 5

您可以使用each_index连同select:

arr = [1,4,6,4,1]
arr.each_index.select { |i| arr[i] == 1 }
#=> [0, 4]
Run Code Online (Sandbox Code Playgroud)