Return `MatchData` with `$~`
Check out this Ruby regex:
2.1.0 :001 > "totally gourmet" =~ /totally/
=> 0
There's a match at index zero. But what is the thing that actually matched? Enter $~
:
2.1.0 :002 > $~
=> #<MatchData "totally">
$~
stores the result of the last regex, and can be called later on. This is equivalent to the match
method:
2.1.0 :003 > /totally/.match('totally gourmet')
=> #<MatchData "totally">
h/t Brian Dunn
Tweet