0

Using Rails 5 and Ruby 2.4. I can't understand why the below is matching . I have an array of strings and I want to see if my string matches any of those in the array, so I tried this

2.4.0 :021 > SEPARATOR_TOKENS = ["-"]
 => ["-"]
2.4.0 :022 > data = "W40"
 => "W40"
2.4.0 :023 > data =~ /[#{Regexp.union(SEPARATOR_TOKENS)}]/
 => 0

even though there is no "-" in my string, it is reporting a match. HOw do I correct this? Note, I have radically sipmlified this example so using an ".include?" is not an option because what I'm doing will eventually include regular expressions.

1 Answer 1

1

Regexp.union already produces a regular expression, you don't need to use /.../

Try this:

data =~ Regexp.union(SEPARATOR_TOKENS)
Sign up to request clarification or add additional context in comments.

1 Comment

K, I oversimplkfied by example so much, your answer is right, but it misses the point of waht I wanted to know. Stay tuned for a new re-formatted question I'm goign to ask if you want to pick up some more cheap points.

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.