Global Match Variables
Until now we’ve been calling the match method and using its return value, or the arguments passed to its block, to discover information about the matched text. But data from the last match is also available, after the match has occurred, in a series of global variables. They’re a little more cryptic, compared to referencing the MatchData directly, but can come in handy—especially when writing Ruby in the shell.
Each one of these global variables contains a different piece of information about the match—things like the specific text that was matched, the contents of any subgroups within the expression, and so on. Let’s explore each one and what it can be used for.
$~
The first global is $~, which contains the entire MatchData ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access