Project

General

Profile

Feature #10851

Introduce Regexp#fetch

Added by prijutme4ty (Ilya Vorontsov) almost 5 years ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:68109]

Description

There is a common pattern in code:
match = pattern.match(string)
var = match && match[name_of_capture]

One should write it everywhere not to get an exception. It can be solved by introducing something like Hash#fetch.

class Regexp
def fetch(string, capture_name:, pos: nil, default_value: nil)
m = match(string, pos)
m ? m[capture_name] : default_value
end
end

Also available in: Atom PDF