1

I am writing a simple program to filter out stock quotes from a given string via a regex. I am not having a problem with the printed results in the array but when I attempt to use "include?" to output a bool of a specific printed result, I am receiving false.

This code demonstrates my exact issue:

http://ideone.com/JiQlA

I have shown the matches, the # of elements, and verified it is an array in the code but when I try to pull the specific printed element "A" using "include?" (line 10), it fails to output "true."

I would very much appreciate if someone would kindly explain my error here or how I would appropriately go about finding if a specific element is in the array. Thank you.

3 Answers 3

8

The elements in your Array are not Strings, they're MatchData which happen to print as Strings. Since String "A" is not equal to #<MatchData "A" 1:nil>, include? fails.

Sign up to request clarification or add additional context in comments.

Comments

5

puts matches.map{|x| x.to_s}.include?("A")

2 Comments

and to see what your error was, replace puts matches with puts matches.inspect
There's a shorter way: matches.map(&:to_s).include?('A')
1

Because matches is an array of MatchData objects, as seen here. You need to convert the elements to strings first, so do that in your map call:

matches = s.to_enum(:scan, r).map { Regexp.last_match.to_s }

Comments

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.