Issue #10851 has been reported by Ilya Vorontsov.
----------------------------------------
Feature #10851: Introduce Regexp#fetch
https://bugs.ruby-lang.org/issues/10851
* Author: Ilya Vorontsov
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
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
--
https://bugs.ruby-lang.org/