Boolean check if a string matches a regex in ruby?
regex, ruby
Solution
You can use this:
def test(s)
/Macintosh/ === s
end
Problem
In ruby, I have seen a lot of code doing things like: ``` def test(s) s =~ /Macintosh/ end ``` to test whether or not a string matches a regex. However, this returns a fixnum or nil, depending on if it finds a match. Is there a way to do the same operation, but have it return a boolean as to whether or not it matched? The two possible solutions that I thought of were `!(s =~ /Macintosh/).nil?` and `!(s !~ /Macintosh/)`, but neither of those seem very readable. Is there something like: ``` def test(s) s.matches?(/Macintosh/) end ```