2

I am basically trying to select every string in an array that matches a particular string.

I am currently using the following code, but as you guys know, this only gives an array of the indexes of the elements that evaluate to true. I want the actual string at that position.

arr.each_index.select{|i| arr[i].chars.sort.join == someString}
1
  • pls provide sample data Commented Jan 30, 2014 at 6:08

1 Answer 1

6

Try this:

arr.select { |s| s == "some string" }

An example:

arr = %w(One Two Three Two Two)

arr.select { |x| x == "Two" }
 => ["Two", "Two", "Two"]
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried this, when I do that I get a Type Error "Can't convert string into integer"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.