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 -
Before
Ruby returns an object of class MatchData
where we can use the []
function
on the result
object.
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 MatchData#[]
method.
Let’s take an example to understand the integer, range, and symbol cases.
To access the length,
we had to chain .length
on []
method of MatchData
class.
After
With the recent change in Ruby 3.1,
we have two methods to access the match data and its length.
Both MatchData#match
and MatchData#match_length
accept either an index
or a symbol as an argument.
So the above example will change as below:
Note:
MatchData#match
is not same as MatchData[]
.
MatchData[]
accepts an integer, range, or symbol as an argument,
but #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.