Ruby 从数组中选择字符串

Sca*_*olo 2 ruby

我基本上是在尝试选择匹配特定字符串的数组中的每个字符串。

我目前正在使用以下代码,但正如你们所知,这仅给出了评估为 true 的元素索引的数组。我想要那个位置的实际字符串。

arr.each_index.select{|i| arr[i].chars.sort.join == someString}
Run Code Online (Sandbox Code Playgroud)

Ben*_*Hao 6

尝试这个:

arr.select { |s| s == "some string" }
Run Code Online (Sandbox Code Playgroud)

一个例子:

arr = %w(One Two Three Two Two)

arr.select { |x| x == "Two" }
 => ["Two", "Two", "Two"]
Run Code Online (Sandbox Code Playgroud)