Ruby 3.1 adds MatchData#match and MatchData#match_length
source link: https://blog.saeloun.com/2021/11/24/ruby-adds-matchdata-match-and-match-length
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Ruby 3.1 adds MatchData#match and MatchData#match_length
Nov 24, 2021 , by Alkesh Ghorpade 1 minute readWhen 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 -
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
result = VALID_EMAIL_REGEX.match("[email protected]")
=> #<MatchData "[email protected]" 1:nil>
result = VALID_EMAIL_REGEX.match("invalid_email")
=> nil
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.
result = /\$(?<dollars>\d+)\.(?<cents>\d+)/.match("$1.95")
=> #<MatchData "$1.95" dollars:"1" cents:"95">
result[0]
=> "$1.95"
result[0..2]
=> ["$1.95", "1", "95"]
result[3]
=> nil
result[:dollars]
=> "1"
result[:cents]
=> "95"
result[:cents].length
=> 2
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:
result = /\$(?<dollars>\d+)\.(?<cents>\d+)/.match("$1.95")
=> #<MatchData "$1.95" dollars:"1" cents:"95">
result.match(0)
=> "$1.95"
result.match(1)
=> "1"
result.match(:dollars)
=> "1"
result.match(:cents)
=> "95"
result.match_length(:cents)
=> 2
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:
result.match(0..2)
(irb):44:in `match': no implicit conversion of Range into Integer (TypeError)
For more discussion related to this change, please refer to this PR.
Recommend
-
69
Don't Step on a Rake, Use Rake::DSL — A look at how Rake, Ruby’s general purpose make-like task runner, operates and how to define tasks outside...
-
15
Ruby adds instance method #except to Hash. This method returns a new hash, which includes everything from the original hash except the given keys. Before...
-
7
Variable-length arrays and the max() mess This article brought to you by LWN subscribersSubscribers to LWN.net made this article — and everything that surrounds it — possible. If you appreciate our...
-
8
Packaging rectangles with a given width and length advertisements I have a tree with rectangles in the leaf nodes, and a 'V' or a 'H' in internal n...
-
7
Ruby 3.1 adds Array#intersect? method May 18, 2021 , by Alkesh Ghorpade 1 minute read When dealing with Arrays in Ruby we fr...
-
5
Ruby 3.1 adds Enumerable#compact and Enumerator::Lazy#compact Jun 15, 2021 , by Keshav Biswa 2 minute read
-
0
Ruby 3.1 adds Class#descendants Dec 22, 2021 , by Vikrant Patel 1 minute read We often land on a si...
-
2
Ruby 3.1 adds Class#subclasses Jan 11, 2022 , by Agnidipta Bhattacharjee 1 minute read We often c...
-
7
Ruby 3.1 adds error highlighting gem Feb 16, 2022 , by Alkesh Ghorpade 2 minute read When working wi...
-
1
2020-08-13 — IBAN length and DESI score Looking at the formats of European country international bank account number...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK