Project

General

Profile

Actions

Feature #14443

closed

Omit 'pattern' parameter in '(g)sub(!)' when 'hash' is given

Added by sawa (Tsuyoshi Sawada) almost 4 years ago. Updated about 2 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:85386]

Description

When (g)sub(!) takes a hash as the second argument, in almost all use cases, the first argument expresses the union of keys of the hash. In the following, /[abc]/ is the union of the keys "a", "b", "c".

"blahblah".sub(/[abc]/, {"a" => "A", "b" => "B", "c" => "C"})

I feel this redundant and not efficient. Hence I propose to let the current first argument be optional when a hash is given. The following:

"blahblah".sub("a" => "A", "b" => "B", "c" => "C")

should be equivalent to:

"blahblah".sub(pattern, {"a" => "A", "b" => "B", "c" => "C"})

where:

pattern = Regexp.union(*{"a" => "A", "b" => "B", "c" => "C"}.keys.map{|k| Regexp.escape(k)})

Related issues

Is duplicate of Ruby master - Feature #13016: String#gsub(hash)RejectedActions
Actions

Also available in: Atom PDF