[ruby-core:68109] [Ruby trunk - Feature #10851] [Open] Introduce Regexp#fetch

1 view
Skip to first unread message

priju...@gmail.com

unread,
Feb 13, 2015, 6:15:33 AM2/13/15
to ruby...@ruby-lang.org
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/
Reply all
Reply to author
Forward
0 new messages