Ruby从数组中选择字符串

Ruby从数组中选择字符串,ruby,Ruby,我基本上是尝试选择数组中与特定字符串匹配的每个字符串 我目前正在使用以下代码,但正如大家所知,这只提供了一个元素的索引数组,这些元素的计算结果为true。我想要那个位置的实际字符串 arr.each_index.select{|i| arr[i].chars.sort.join == someString} 试试这个: arr.select { |s| s == "some string" } 例如: arr = %w(One Two Three Two Two) arr.select {

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

我目前正在使用以下代码,但正如大家所知,这只提供了一个元素的索引数组,这些元素的计算结果为true。我想要那个位置的实际字符串

arr.each_index.select{|i| arr[i].chars.sort.join == someString}
试试这个:

arr.select { |s| s == "some string" }
例如:

arr = %w(One Two Three Two Two)

arr.select { |x| x == "Two" }
 => ["Two", "Two", "Two"]

我尝试过这个,当我这样做,我得到一个类型错误“不能转换成整数字符串”请提供样本数据