This blog is part of our Ruby 2.4 series.
Ruby has many ways to match with a regular expression.
It returns true/false and sets the $~ global variable.
It returns integer position it matched or nil if no match. It also sets the $~ global variable.
It returns match data and also sets the $~ global variable.
Ruby 2.4 adds Regexp#match?
This new method just returns true/false and does not set any global variables.
Regexp#match? is good option when we are only concerned with the fact that regex matches or not.
Regexp#match? is also faster than its counterparts
as it reduces object allocation by not creating a back reference and