When working with strings, we come across cases where we need to match string characters or words using regular expressions. We use regular expressions widely for matching email and phone numbers formats.
A regex for a valid email address is as below -
Ruby returns an object of class
MatchData where we can use the
 function will expect either an index or symbol as an argument.
We can also access multiple matches by passing a range of indexes to the
Let’s take an example to understand the integer, range, and symbol cases.
To access the length,
we had to chain
 method of
With the recent change in Ruby 3.1,
we have two methods to access the match data and its length.
MatchData#match_length accept either an index
or a symbol as an argument.
So the above example will change as below:
MatchData#match is not same as
MatchData accepts an integer, range, or symbol as an argument,
#match allows only a single index or symbol.
Passing a range to match method will raise an error as below:
For more discussion related to this change, please refer to this PR.