Project

General

Profile

Actions

Feature #10851

open

Introduce Regexp#fetch

Added by prijutme4ty (Ilya Vorontsov) about 9 years ago.

Status:
Open
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

No data to display

Actions

Also available in: Atom PDF

Like0