0

I have a array of few string

For ex: ["hello", "World", "nice"]

Now i have a long url string with special string.

I want to find out which string from above array is present in my url string using Ruby and return that string.

3
  • Use each and check to see if it's in the URL. What's the specific issue? What have you tried so far? Commented Apr 11, 2014 at 15:30
  • i want to avoid each and want some useful regex which can give me that substring in less time. Commented Apr 11, 2014 at 15:31
  • And you've determined this is a relevant performance bottleneck in your system??? So join them together with a | and do a regex. Then do the relevant performance tests. Then ponder if the time you wasted was actually worth it. Commented Apr 11, 2014 at 15:58

1 Answer 1

2

Use find:

words.find { |w| url[w] }

Example:

 ["hello", "World", "nice"].find { |w| "this is nice"[w] }
 # => "nice"
Sign up to request clarification or add additional context in comments.

1 Comment

+1, I came here to post this! To add to your answer, note that find/detect will return the first match only. The methods find_all/select are available if one needs all the values that match.

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.