Project

General

Profile

Actions

Feature #14443

closed

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

Added by sawa (Tsuyoshi Sawada) about 6 years ago. Updated over 2 years ago.

Status:
Closed
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 1 (0 open1 closed)

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

Updated by sawa (Tsuyoshi Sawada) about 6 years ago

If creation of the union is done within C-Ruby code, I believe that should be more efficient than having the pattern argument explicitly given and interpreted.

Actions #2

Updated by shyouhei (Shyouhei Urabe) about 6 years ago

Updated by shyouhei (Shyouhei Urabe) about 6 years ago

I remember this was rejected.

Updated by shevegen (Robert A. Heiler) about 6 years ago

To quote from the other issue and what shyouhei wrote there:

"While I claimed the use of regular expression is an implementation detail that I don't want to care about, attendees there said it is better to expose compiled structure (be they regexp) for performance. I agree with that, so I give up this propossal."

Actions #5

Updated by jeremyevans0 (Jeremy Evans) over 2 years ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0