Issue #13016 has been updated by Martin Drst.


Shyouhei Urabe wrote:

> It seems there are quite few resources describing this feature on line.

Thanks!


> That still sounds like a hustle to me.

Can you write the last sentence in Japanese? The word 'hustle' has lots of meanings, some of them confusing.


> The proposed functionality would make it a lot easier for me to emulate NKF's Z4.

I agree it would make it easier. But I'm not sure about "a lot". The main work needed to implement it is to create the hash. My understanding is that you still have to do that by hand. My suggestion would be to use literal characters, not \u escapes, in most cases because that makes it much easier to spot errors.

Compared to creating the hash, the shortening from

```
str.gsub(Regexp.union(map.keys)) { map[$&] }
```

to

```
str.gsub(map)
```

seems to be a minor simplification, and one that is easily done by defining a new method:

```
class String
  def hsub(map)
    gsub(Regexp.union(map.keys)) { map[$&] }
  end
end
```

I'm not against this feature, but I think it would be good to have some more examples of where it could be useful, and some check that we don't want to use String#gmap(Hash) with some other meaning in the future.

----------------------------------------
Feature #13016: String#gsub(hash)
https://bugs.ruby-lang.org/issues/13016#change-61932

* Author: Shyouhei Urabe
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
Background: I wanted to drop NKF dependency of my script.  By doing so I noticed that I can't purge `NKF.nkf '-Z4'`.  It can neither be rewritten using String#tr, String#encode, nor String#unicode_normalize. It is doable using String#gsub theoretically, but that requires a hand-crafted nontrivial regular expression that exactly matches what Z4 expects to convert.  This is almost impossible to do, and is definitely not something debuggable.

Proposal: extend String#gsub so that it also accepts hash as its only argument, specifying input-output mapping.

```ruby
# now
def convert str
  require 'nkf'
  NKF.nkf '-Z4xm0', str
end

# proposed
def convert str
  map = {  "\u3002" => "\uFF61", "\u300C" => "\uFF62", ... }
  str.gsub map
end
```



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>