This blog is part of our Ruby 2.4 series.
Ruby has MatchData type which
is returned by Regexp#match and Regexp.last_match.
It has methods #names and #captures to return the names used for capturing
and the actual captured data respectively.
pattern = /(?<number>\d+) (?<word>\w+)/
match_data = pattern.match('100 thousand')
#=> #<MatchData "100 thousand" number:"100" word:"thousand">
>> match_data.names
=> ["number", "word"]
>> match_data.captures
=> ["100", "thousand"]
If we want all named captures in a key value pair, we have to combine the result of names and captures.
match_data.names.zip(match_data.captures).to_h
#=> {"number"=>"100", "word"=>"thousand"}
Ruby 2.4 adds #named_captures which
returns both the name and data of the capture groups.
pattern=/(?<number>\d+) (?<word>\w+)/
match_data = pattern.match('100 thousand')
match_data.named_captures
#=> {"number"=>"100", "word"=>"thousand"}

Written byRohit Kumar
Related blogs
- 01Ruby 2.4 has optimized enumerable min max methodsSeptember 28, 2017 · Chirag Shah
- 02CSV::Row#each etc. return enumerator when no block givenSeptember 25, 2017 · Sushant Mittal
- 03Ruby 2.4 DateTime#to_time & Time#to_time keeps infoSeptember 19, 2017 · Sushant Mittal
- 04Avoid exception for dup on IntegerAugust 1, 2017 · Rohit Arolkar