ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-dev:50445] [Ruby trunk Feature#14417] String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
       [not found] <redmine.issue-14417.20180129093502@ruby-lang.org>
@ 2018-01-29  9:35 ` manga.osyo
  2018-01-29  9:56 ` [ruby-dev:50446] " hanmac
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: manga.osyo @ 2018-01-29  9:35 UTC (permalink / raw)
  To: ruby-dev

Issue #14417 has been reported by osyo (manga osyo).

----------------------------------------
Feature #14417: String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
https://bugs.ruby-lang.org/issues/14417

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## 概要

`String#sub` / `String#gsub` に『キーが `Symbol` の `Hash`』を渡した場合でも `String` の場合と同様に置き換える。


## 現行の動作

```ruby
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

# キー が Symbol の Hash は置き換えられない
hash = { b: 'B', c: 'C' }
p "abcabc".gsub(/[bc]/, hash)     #=> "aa"

# キー が Symbol の Hash は String に変換する必要がある
p "abcabc".gsub(/[bc]/, hash.transform_keys(&:to_s))     #=> "aBCaBC"
```


## 提案する動作

```ruby
# キーが String の場合は現行維持
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

hash = { b: 'B', c: 'C' }

# $& は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){hash[$&]} #=> "aa"

# ブロックの引数は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){ |s| hash[s] } #=> a"

# Hash を直接渡した場合のみキーが Symbol でも許容する
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"
```


## 利点

* キーを Symbol で書くことを推奨しているコーディング規約がある
  * [ruby-style-guide/README.ja.md at japanese · fortissimo1997/ruby-style-guide · GitHub](https://github.com/fortissimo1997/ruby-style-guide/blob/japanese/README.ja.md#symbols-as-keys)
* キーを Symbol で定義する方が Hash を書いていて気持ちがいい


## 課題

* `String` と `Symbol` の両方のキーがあった場合どうするか
  * `"abcabc".sub(/[bc]/, { "b" => "A", b: "C" })  # => ???`
  * 現状は `String` を優先している
  * それ以前に `String` と `Symbol` が混ざっている Hash はおかしいのではないだろうか
  * 警告を出すとか?


## `String#gsub` のユースケースなど

```ruby
# http://batsov.com/articles/2013/10/03/using-rubys-gsub-with-a-hash/
def geekify(string)
  string.gsub(/[leto]/, l: '1', e: '3', t: '7', o: '0')
end

p geekify('leet') # => '1337'
p geekify('noob') # => 'n00b'


def doctorize(string)
  string.gsub(/M(iste)?r/, Mister: 'Doctor', Mr: 'Dr')
end

p doctorize('Mister Freeze') # => 'Doctor Freeze'
p doctorize('Mr Smith')   # => 'Dr Smith'
```

```ruby
# https://coderwall.com/p/t4y7cw/ruby-gsub-with-a-hash-or-block
amino_acid_hash = { A: 'Ala', R: 'Arg', N: 'Asn' }

p "R232A".gsub(/[A-Z]/, amino_acid_hash)
# => "Arg232Ala"
```

```ruby
# https://qiita.com/scivola/items/416155c307ec29a37b8f
hash = {
  '&': "&amp",
  '<': "&lt",
  '>': "&gt",
}

p "<Q&A>".gsub(/[&<>]/, hash)
# => "&ltQ&ampA&gt"
```

```ruby
# https://qiita.com/pocari/items/34855a9b07ea5006fe80
hash = {
  '#to#': "taro",
  '#from#': "jiro",
}

template = <<EOS
hello, #to#.
message from #from#.
EOS

puts template.gsub(/#.*#/, hash)
# => hello, taro.
# message from jiro.
```

その他、具体的なユースケースを思いついた方がいればコメントいただけると助かります。

---Files--------------------------------
string_sub_with_symbol_key.patch (2.71 KB)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-dev:50446] [Ruby trunk Feature#14417] String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
       [not found] <redmine.issue-14417.20180129093502@ruby-lang.org>
  2018-01-29  9:35 ` [ruby-dev:50445] [Ruby trunk Feature#14417] String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案 manga.osyo
@ 2018-01-29  9:56 ` hanmac
  2018-01-29  9:59 ` [ruby-dev:50447] [Ruby trunk Feature#14417][Feedback] " shyouhei
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: hanmac @ 2018-01-29  9:56 UTC (permalink / raw)
  To: ruby-dev

Issue #14417 has been updated by Hanmac (Hans Mackowiak).


even if Ruby Symbols are freed now, i still have some problems with that it creates that much symbols from possible tainted string data

would it probably better if sub/gsub would call `hash.transform_keys(&:to_s)` internal in their code with the hash if hash is given?

if yes then this would work too:

~~~ ruby
"12345".gsub(/\d/,{"1" => "A", "2" => "B", "3" => "C", "4" => "D", "5" => "E"}) #=> "ABCDE"
"12345".gsub(/\d/,{1 => "A", 2 => "B", 3 => "C", 4 => "D", 5 => "E"}) #=> "ABCDE"
~~~


----------------------------------------
Feature #14417: String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
https://bugs.ruby-lang.org/issues/14417#change-69965

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## 概要

`String#sub` / `String#gsub` に『キーが `Symbol` の `Hash`』を渡した場合でも `String` の場合と同様に置き換える。


## 現行の動作

```ruby
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

# キー が Symbol の Hash は置き換えられない
hash = { b: 'B', c: 'C' }
p "abcabc".gsub(/[bc]/, hash)     #=> "aa"

# キー が Symbol の Hash は String に変換する必要がある
p "abcabc".gsub(/[bc]/, hash.transform_keys(&:to_s))     #=> "aBCaBC"
```


## 提案する動作

```ruby
# キーが String の場合は現行維持
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

hash = { b: 'B', c: 'C' }

# $& は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){hash[$&]} #=> "aa"

# ブロックの引数は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){ |s| hash[s] } #=> a"

# Hash を直接渡した場合のみキーが Symbol でも許容する
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"
```


## 利点

* キーを Symbol で書くことを推奨しているコーディング規約がある
  * [ruby-style-guide/README.ja.md at japanese · fortissimo1997/ruby-style-guide · GitHub](https://github.com/fortissimo1997/ruby-style-guide/blob/japanese/README.ja.md#symbols-as-keys)
* キーを Symbol で定義する方が Hash を書いていて気持ちがいい


## 課題

* `String` と `Symbol` の両方のキーがあった場合どうするか
  * `"abcabc".sub(/[bc]/, { "b" => "A", b: "C" })  # => ???`
  * 現状は `String` を優先している
  * それ以前に `String` と `Symbol` が混ざっている Hash はおかしいのではないだろうか
  * 警告を出すとか?


## `String#gsub` のユースケースなど

```ruby
# http://batsov.com/articles/2013/10/03/using-rubys-gsub-with-a-hash/
def geekify(string)
  string.gsub(/[leto]/, l: '1', e: '3', t: '7', o: '0')
end

p geekify('leet') # => '1337'
p geekify('noob') # => 'n00b'


def doctorize(string)
  string.gsub(/M(iste)?r/, Mister: 'Doctor', Mr: 'Dr')
end

p doctorize('Mister Freeze') # => 'Doctor Freeze'
p doctorize('Mr Smith')   # => 'Dr Smith'
```

```ruby
# https://coderwall.com/p/t4y7cw/ruby-gsub-with-a-hash-or-block
amino_acid_hash = { A: 'Ala', R: 'Arg', N: 'Asn' }

p "R232A".gsub(/[A-Z]/, amino_acid_hash)
# => "Arg232Ala"
```

```ruby
# https://qiita.com/scivola/items/416155c307ec29a37b8f
hash = {
  '&': "&amp",
  '<': "&lt",
  '>': "&gt",
}

p "<Q&A>".gsub(/[&<>]/, hash)
# => "&ltQ&ampA&gt"
```

```ruby
# https://qiita.com/pocari/items/34855a9b07ea5006fe80
hash = {
  '#to#': "taro",
  '#from#': "jiro",
}

template = <<EOS
hello, #to#.
message from #from#.
EOS

puts template.gsub(/#.*#/, hash)
# => hello, taro.
# message from jiro.
```

その他、具体的なユースケースを思いついた方がいればコメントいただけると助かります。

---Files--------------------------------
string_sub_with_symbol_key.patch (2.71 KB)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-dev:50447] [Ruby trunk Feature#14417][Feedback] String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
       [not found] <redmine.issue-14417.20180129093502@ruby-lang.org>
  2018-01-29  9:35 ` [ruby-dev:50445] [Ruby trunk Feature#14417] String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案 manga.osyo
  2018-01-29  9:56 ` [ruby-dev:50446] " hanmac
@ 2018-01-29  9:59 ` shyouhei
  2018-01-29 10:51 ` [ruby-dev:50448] [Ruby trunk Feature#14417] " manga.osyo
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: shyouhei @ 2018-01-29  9:59 UTC (permalink / raw)
  To: ruby-dev

Issue #14417 has been updated by shyouhei (Shyouhei Urabe).

Status changed from Open to Feedback

提案されている利点は弱すぎて賛成しがたいです(趣味では)。
とはいえ機能自体に反対ではないですから、より具体的なユースケースがあると賛成しやすくなるかなと思います。

----------------------------------------
Feature #14417: String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
https://bugs.ruby-lang.org/issues/14417#change-69966

* Author: osyo (manga osyo)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## 概要

`String#sub` / `String#gsub` に『キーが `Symbol` の `Hash`』を渡した場合でも `String` の場合と同様に置き換える。


## 現行の動作

```ruby
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

# キー が Symbol の Hash は置き換えられない
hash = { b: 'B', c: 'C' }
p "abcabc".gsub(/[bc]/, hash)     #=> "aa"

# キー が Symbol の Hash は String に変換する必要がある
p "abcabc".gsub(/[bc]/, hash.transform_keys(&:to_s))     #=> "aBCaBC"
```


## 提案する動作

```ruby
# キーが String の場合は現行維持
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

hash = { b: 'B', c: 'C' }

# $& は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){hash[$&]} #=> "aa"

# ブロックの引数は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){ |s| hash[s] } #=> a"

# Hash を直接渡した場合のみキーが Symbol でも許容する
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"
```


## 利点

* キーを Symbol で書くことを推奨しているコーディング規約がある
  * [ruby-style-guide/README.ja.md at japanese · fortissimo1997/ruby-style-guide · GitHub](https://github.com/fortissimo1997/ruby-style-guide/blob/japanese/README.ja.md#symbols-as-keys)
* キーを Symbol で定義する方が Hash を書いていて気持ちがいい


## 課題

* `String` と `Symbol` の両方のキーがあった場合どうするか
  * `"abcabc".sub(/[bc]/, { "b" => "A", b: "C" })  # => ???`
  * 現状は `String` を優先している
  * それ以前に `String` と `Symbol` が混ざっている Hash はおかしいのではないだろうか
  * 警告を出すとか?


## `String#gsub` のユースケースなど

```ruby
# http://batsov.com/articles/2013/10/03/using-rubys-gsub-with-a-hash/
def geekify(string)
  string.gsub(/[leto]/, l: '1', e: '3', t: '7', o: '0')
end

p geekify('leet') # => '1337'
p geekify('noob') # => 'n00b'


def doctorize(string)
  string.gsub(/M(iste)?r/, Mister: 'Doctor', Mr: 'Dr')
end

p doctorize('Mister Freeze') # => 'Doctor Freeze'
p doctorize('Mr Smith')   # => 'Dr Smith'
```

```ruby
# https://coderwall.com/p/t4y7cw/ruby-gsub-with-a-hash-or-block
amino_acid_hash = { A: 'Ala', R: 'Arg', N: 'Asn' }

p "R232A".gsub(/[A-Z]/, amino_acid_hash)
# => "Arg232Ala"
```

```ruby
# https://qiita.com/scivola/items/416155c307ec29a37b8f
hash = {
  '&': "&amp",
  '<': "&lt",
  '>': "&gt",
}

p "<Q&A>".gsub(/[&<>]/, hash)
# => "&ltQ&ampA&gt"
```

```ruby
# https://qiita.com/pocari/items/34855a9b07ea5006fe80
hash = {
  '#to#': "taro",
  '#from#': "jiro",
}

template = <<EOS
hello, #to#.
message from #from#.
EOS

puts template.gsub(/#.*#/, hash)
# => hello, taro.
# message from jiro.
```

その他、具体的なユースケースを思いついた方がいればコメントいただけると助かります。

---Files--------------------------------
string_sub_with_symbol_key.patch (2.71 KB)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-dev:50448] [Ruby trunk Feature#14417] String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
       [not found] <redmine.issue-14417.20180129093502@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2018-01-29  9:59 ` [ruby-dev:50447] [Ruby trunk Feature#14417][Feedback] " shyouhei
@ 2018-01-29 10:51 ` manga.osyo
  2018-01-29 15:17 ` [ruby-dev:50449] " hanmac
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: manga.osyo @ 2018-01-29 10:51 UTC (permalink / raw)
  To: ruby-dev

Issue #14417 has been updated by osyo (manga osyo).


Thanks for reply!!!

> would it probably better if sub/gsub would call hash.transform_keys(&:to_s) internal in their code with the hash if hash is given?

hmmmm... Thanks for idea :)

> とはいえ機能自体に反対ではないですから、より具体的なユースケースがあると賛成しやすくなるかなと思います。

そうですねえ…もう少し具体的なユースケースを考えてみたいと思います。
コメントありがとうございます。


----------------------------------------
Feature #14417: String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
https://bugs.ruby-lang.org/issues/14417#change-69967

* Author: osyo (manga osyo)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## 概要

`String#sub` / `String#gsub` に『キーが `Symbol` の `Hash`』を渡した場合でも `String` の場合と同様に置き換える。


## 現行の動作

```ruby
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

# キー が Symbol の Hash は置き換えられない
hash = { b: 'B', c: 'C' }
p "abcabc".gsub(/[bc]/, hash)     #=> "aa"

# キー が Symbol の Hash は String に変換する必要がある
p "abcabc".gsub(/[bc]/, hash.transform_keys(&:to_s))     #=> "aBCaBC"
```


## 提案する動作

```ruby
# キーが String の場合は現行維持
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

hash = { b: 'B', c: 'C' }

# $& は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){hash[$&]} #=> "aa"

# ブロックの引数は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){ |s| hash[s] } #=> a"

# Hash を直接渡した場合のみキーが Symbol でも許容する
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"
```


## 利点

* キーを Symbol で書くことを推奨しているコーディング規約がある
  * [ruby-style-guide/README.ja.md at japanese · fortissimo1997/ruby-style-guide · GitHub](https://github.com/fortissimo1997/ruby-style-guide/blob/japanese/README.ja.md#symbols-as-keys)
* キーを Symbol で定義する方が Hash を書いていて気持ちがいい


## 課題

* `String` と `Symbol` の両方のキーがあった場合どうするか
  * `"abcabc".sub(/[bc]/, { "b" => "A", b: "C" })  # => ???`
  * 現状は `String` を優先している
  * それ以前に `String` と `Symbol` が混ざっている Hash はおかしいのではないだろうか
  * 警告を出すとか?


## `String#gsub` のユースケースなど

```ruby
# http://batsov.com/articles/2013/10/03/using-rubys-gsub-with-a-hash/
def geekify(string)
  string.gsub(/[leto]/, l: '1', e: '3', t: '7', o: '0')
end

p geekify('leet') # => '1337'
p geekify('noob') # => 'n00b'


def doctorize(string)
  string.gsub(/M(iste)?r/, Mister: 'Doctor', Mr: 'Dr')
end

p doctorize('Mister Freeze') # => 'Doctor Freeze'
p doctorize('Mr Smith')   # => 'Dr Smith'
```

```ruby
# https://coderwall.com/p/t4y7cw/ruby-gsub-with-a-hash-or-block
amino_acid_hash = { A: 'Ala', R: 'Arg', N: 'Asn' }

p "R232A".gsub(/[A-Z]/, amino_acid_hash)
# => "Arg232Ala"
```

```ruby
# https://qiita.com/scivola/items/416155c307ec29a37b8f
hash = {
  '&': "&amp",
  '<': "&lt",
  '>': "&gt",
}

p "<Q&A>".gsub(/[&<>]/, hash)
# => "&ltQ&ampA&gt"
```

```ruby
# https://qiita.com/pocari/items/34855a9b07ea5006fe80
hash = {
  '#to#': "taro",
  '#from#': "jiro",
}

template = <<EOS
hello, #to#.
message from #from#.
EOS

puts template.gsub(/#.*#/, hash)
# => hello, taro.
# message from jiro.
```

その他、具体的なユースケースを思いついた方がいればコメントいただけると助かります。

---Files--------------------------------
string_sub_with_symbol_key.patch (2.71 KB)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-dev:50449] [Ruby trunk Feature#14417] String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
       [not found] <redmine.issue-14417.20180129093502@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2018-01-29 10:51 ` [ruby-dev:50448] [Ruby trunk Feature#14417] " manga.osyo
@ 2018-01-29 15:17 ` hanmac
  2018-01-31 11:06 ` [ruby-dev:50454] " duerst
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 8+ messages in thread
From: hanmac @ 2018-01-29 15:17 UTC (permalink / raw)
  To: ruby-dev

Issue #14417 has been updated by Hanmac (Hans Mackowiak).


did look at string.c for gsub code,
https://github.com/ruby/ruby/blob/trunk/string.c#L5094
seems to be the line where we could add a transform_keys call

but i don't know currently what the best way to call `hash.transform_keys(&:to_s)`
probably something with `rb_funcall_with_block`?

----------------------------------------
Feature #14417: String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
https://bugs.ruby-lang.org/issues/14417#change-69969

* Author: osyo (manga osyo)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## 概要

`String#sub` / `String#gsub` に『キーが `Symbol` の `Hash`』を渡した場合でも `String` の場合と同様に置き換える。


## 現行の動作

```ruby
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

# キー が Symbol の Hash は置き換えられない
hash = { b: 'B', c: 'C' }
p "abcabc".gsub(/[bc]/, hash)     #=> "aa"

# キー が Symbol の Hash は String に変換する必要がある
p "abcabc".gsub(/[bc]/, hash.transform_keys(&:to_s))     #=> "aBCaBC"
```


## 提案する動作

```ruby
# キーが String の場合は現行維持
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

hash = { b: 'B', c: 'C' }

# $& は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){hash[$&]} #=> "aa"

# ブロックの引数は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){ |s| hash[s] } #=> a"

# Hash を直接渡した場合のみキーが Symbol でも許容する
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"
```


## 利点

* キーを Symbol で書くことを推奨しているコーディング規約がある
  * [ruby-style-guide/README.ja.md at japanese · fortissimo1997/ruby-style-guide · GitHub](https://github.com/fortissimo1997/ruby-style-guide/blob/japanese/README.ja.md#symbols-as-keys)
* キーを Symbol で定義する方が Hash を書いていて気持ちがいい


## 課題

* `String` と `Symbol` の両方のキーがあった場合どうするか
  * `"abcabc".sub(/[bc]/, { "b" => "A", b: "C" })  # => ???`
  * 現状は `String` を優先している
  * それ以前に `String` と `Symbol` が混ざっている Hash はおかしいのではないだろうか
  * 警告を出すとか?


## `String#gsub` のユースケースなど

```ruby
# http://batsov.com/articles/2013/10/03/using-rubys-gsub-with-a-hash/
def geekify(string)
  string.gsub(/[leto]/, l: '1', e: '3', t: '7', o: '0')
end

p geekify('leet') # => '1337'
p geekify('noob') # => 'n00b'


def doctorize(string)
  string.gsub(/M(iste)?r/, Mister: 'Doctor', Mr: 'Dr')
end

p doctorize('Mister Freeze') # => 'Doctor Freeze'
p doctorize('Mr Smith')   # => 'Dr Smith'
```

```ruby
# https://coderwall.com/p/t4y7cw/ruby-gsub-with-a-hash-or-block
amino_acid_hash = { A: 'Ala', R: 'Arg', N: 'Asn' }

p "R232A".gsub(/[A-Z]/, amino_acid_hash)
# => "Arg232Ala"
```

```ruby
# https://qiita.com/scivola/items/416155c307ec29a37b8f
hash = {
  '&': "&amp",
  '<': "&lt",
  '>': "&gt",
}

p "<Q&A>".gsub(/[&<>]/, hash)
# => "&ltQ&ampA&gt"
```

```ruby
# https://qiita.com/pocari/items/34855a9b07ea5006fe80
hash = {
  '#to#': "taro",
  '#from#': "jiro",
}

template = <<EOS
hello, #to#.
message from #from#.
EOS

puts template.gsub(/#.*#/, hash)
# => hello, taro.
# message from jiro.
```

その他、具体的なユースケースを思いついた方がいればコメントいただけると助かります。

---Files--------------------------------
string_sub_with_symbol_key.patch (2.71 KB)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-dev:50454] [Ruby trunk Feature#14417] String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
       [not found] <redmine.issue-14417.20180129093502@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2018-01-29 15:17 ` [ruby-dev:50449] " hanmac
@ 2018-01-31 11:06 ` duerst
  2018-01-31 11:17 ` [ruby-dev:50455] " hanmac
  2018-02-04 13:05 ` [ruby-dev:50467] " naruse
  7 siblings, 0 replies; 8+ messages in thread
From: duerst @ 2018-01-31 11:06 UTC (permalink / raw)
  To: ruby-dev

Issue #14417 has been updated by duerst (Martin Dürst).


`gsub` with Hash is used in some contexts where high performance is of interest. An example is `lib/unicode_normalize/normalize.rb`. This proposal would make these cases less efficient, for the benefit of people who can't keep Symbols and Strings apart.

As discussed in another issue, `b: 'B', c: 'C'` is not a shortcut for `'b'=>'B', 'c'=>'C'`. We already have methods to change Hash keys (or values), and we probably need more of them, but I think we don't need more methods that accepts strings and symbols indeterminately.

----------------------------------------
Feature #14417: String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
https://bugs.ruby-lang.org/issues/14417#change-70068

* Author: osyo (manga osyo)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## 概要

`String#sub` / `String#gsub` に『キーが `Symbol` の `Hash`』を渡した場合でも `String` の場合と同様に置き換える。


## 現行の動作

```ruby
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

# キー が Symbol の Hash は置き換えられない
hash = { b: 'B', c: 'C' }
p "abcabc".gsub(/[bc]/, hash)     #=> "aa"

# キー が Symbol の Hash は String に変換する必要がある
p "abcabc".gsub(/[bc]/, hash.transform_keys(&:to_s))     #=> "aBCaBC"
```


## 提案する動作

```ruby
# キーが String の場合は現行維持
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

hash = { b: 'B', c: 'C' }

# $& は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){hash[$&]} #=> "aa"

# ブロックの引数は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){ |s| hash[s] } #=> a"

# Hash を直接渡した場合のみキーが Symbol でも許容する
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"
```


## 利点

* キーを Symbol で書くことを推奨しているコーディング規約がある
  * [ruby-style-guide/README.ja.md at japanese · fortissimo1997/ruby-style-guide · GitHub](https://github.com/fortissimo1997/ruby-style-guide/blob/japanese/README.ja.md#symbols-as-keys)
* キーを Symbol で定義する方が Hash を書いていて気持ちがいい


## 課題

* `String` と `Symbol` の両方のキーがあった場合どうするか
  * `"abcabc".sub(/[bc]/, { "b" => "A", b: "C" })  # => ???`
  * 現状は `String` を優先している
  * それ以前に `String` と `Symbol` が混ざっている Hash はおかしいのではないだろうか
  * 警告を出すとか?


## `String#gsub` のユースケースなど

```ruby
# http://batsov.com/articles/2013/10/03/using-rubys-gsub-with-a-hash/
def geekify(string)
  string.gsub(/[leto]/, l: '1', e: '3', t: '7', o: '0')
end

p geekify('leet') # => '1337'
p geekify('noob') # => 'n00b'


def doctorize(string)
  string.gsub(/M(iste)?r/, Mister: 'Doctor', Mr: 'Dr')
end

p doctorize('Mister Freeze') # => 'Doctor Freeze'
p doctorize('Mr Smith')   # => 'Dr Smith'
```

```ruby
# https://coderwall.com/p/t4y7cw/ruby-gsub-with-a-hash-or-block
amino_acid_hash = { A: 'Ala', R: 'Arg', N: 'Asn' }

p "R232A".gsub(/[A-Z]/, amino_acid_hash)
# => "Arg232Ala"
```

```ruby
# https://qiita.com/scivola/items/416155c307ec29a37b8f
hash = {
  '&': "&amp",
  '<': "&lt",
  '>': "&gt",
}

p "<Q&A>".gsub(/[&<>]/, hash)
# => "&ltQ&ampA&gt"
```

```ruby
# https://qiita.com/pocari/items/34855a9b07ea5006fe80
hash = {
  '#to#': "taro",
  '#from#': "jiro",
}

template = <<EOS
hello, #to#.
message from #from#.
EOS

puts template.gsub(/#.*#/, hash)
# => hello, taro.
# message from jiro.
```

その他、具体的なユースケースを思いついた方がいればコメントいただけると助かります。

---Files--------------------------------
string_sub_with_symbol_key.patch (2.71 KB)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-dev:50455] [Ruby trunk Feature#14417] String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
       [not found] <redmine.issue-14417.20180129093502@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2018-01-31 11:06 ` [ruby-dev:50454] " duerst
@ 2018-01-31 11:17 ` hanmac
  2018-02-04 13:05 ` [ruby-dev:50467] " naruse
  7 siblings, 0 replies; 8+ messages in thread
From: hanmac @ 2018-01-31 11:17 UTC (permalink / raw)
  To: ruby-dev

Issue #14417 has been updated by Hanmac (Hans Mackowiak).


@duerst: what about my example where it does transform the keye internal for the given Hash?
or is that a nono too?
it might be possible to only do it if the given hash has non String key?

----------------------------------------
Feature #14417: String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
https://bugs.ruby-lang.org/issues/14417#change-70071

* Author: osyo (manga osyo)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## 概要

`String#sub` / `String#gsub` に『キーが `Symbol` の `Hash`』を渡した場合でも `String` の場合と同様に置き換える。


## 現行の動作

```ruby
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

# キー が Symbol の Hash は置き換えられない
hash = { b: 'B', c: 'C' }
p "abcabc".gsub(/[bc]/, hash)     #=> "aa"

# キー が Symbol の Hash は String に変換する必要がある
p "abcabc".gsub(/[bc]/, hash.transform_keys(&:to_s))     #=> "aBCaBC"
```


## 提案する動作

```ruby
# キーが String の場合は現行維持
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

hash = { b: 'B', c: 'C' }

# $& は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){hash[$&]} #=> "aa"

# ブロックの引数は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){ |s| hash[s] } #=> a"

# Hash を直接渡した場合のみキーが Symbol でも許容する
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"
```


## 利点

* キーを Symbol で書くことを推奨しているコーディング規約がある
  * [ruby-style-guide/README.ja.md at japanese · fortissimo1997/ruby-style-guide · GitHub](https://github.com/fortissimo1997/ruby-style-guide/blob/japanese/README.ja.md#symbols-as-keys)
* キーを Symbol で定義する方が Hash を書いていて気持ちがいい


## 課題

* `String` と `Symbol` の両方のキーがあった場合どうするか
  * `"abcabc".sub(/[bc]/, { "b" => "A", b: "C" })  # => ???`
  * 現状は `String` を優先している
  * それ以前に `String` と `Symbol` が混ざっている Hash はおかしいのではないだろうか
  * 警告を出すとか?


## `String#gsub` のユースケースなど

```ruby
# http://batsov.com/articles/2013/10/03/using-rubys-gsub-with-a-hash/
def geekify(string)
  string.gsub(/[leto]/, l: '1', e: '3', t: '7', o: '0')
end

p geekify('leet') # => '1337'
p geekify('noob') # => 'n00b'


def doctorize(string)
  string.gsub(/M(iste)?r/, Mister: 'Doctor', Mr: 'Dr')
end

p doctorize('Mister Freeze') # => 'Doctor Freeze'
p doctorize('Mr Smith')   # => 'Dr Smith'
```

```ruby
# https://coderwall.com/p/t4y7cw/ruby-gsub-with-a-hash-or-block
amino_acid_hash = { A: 'Ala', R: 'Arg', N: 'Asn' }

p "R232A".gsub(/[A-Z]/, amino_acid_hash)
# => "Arg232Ala"
```

```ruby
# https://qiita.com/scivola/items/416155c307ec29a37b8f
hash = {
  '&': "&amp",
  '<': "&lt",
  '>': "&gt",
}

p "<Q&A>".gsub(/[&<>]/, hash)
# => "&ltQ&ampA&gt"
```

```ruby
# https://qiita.com/pocari/items/34855a9b07ea5006fe80
hash = {
  '#to#': "taro",
  '#from#': "jiro",
}

template = <<EOS
hello, #to#.
message from #from#.
EOS

puts template.gsub(/#.*#/, hash)
# => hello, taro.
# message from jiro.
```

その他、具体的なユースケースを思いついた方がいればコメントいただけると助かります。

---Files--------------------------------
string_sub_with_symbol_key.patch (2.71 KB)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [ruby-dev:50467] [Ruby trunk Feature#14417] String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
       [not found] <redmine.issue-14417.20180129093502@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2018-01-31 11:17 ` [ruby-dev:50455] " hanmac
@ 2018-02-04 13:05 ` naruse
  7 siblings, 0 replies; 8+ messages in thread
From: naruse @ 2018-02-04 13:05 UTC (permalink / raw)
  To: ruby-dev

Issue #14417 has been updated by naruse (Yui NARUSE).


Hanmac (Hans Mackowiak) wrote:
> @duerst: what about my example where it does transform the keye internal for the given Hash?
> or is that a nono too?
> it might be possible to only do it if the given hash has non String key?

If the hash is called many times from gsub, those integers shold be converted as String before gsub.
Because such conversion needs object allocation many times, and cause many GC.

I think

```
h = {1 => "A", 2 => "B", 3 => "C", 4 => "D", 5 => "E"}
"12345".gsub(/\d/){ h[$&.to_i] }
```

is faster than such code.

----------------------------------------
Feature #14417: String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案
https://bugs.ruby-lang.org/issues/14417#change-70157

* Author: osyo (manga osyo)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## 概要

`String#sub` / `String#gsub` に『キーが `Symbol` の `Hash`』を渡した場合でも `String` の場合と同様に置き換える。


## 現行の動作

```ruby
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

# キー が Symbol の Hash は置き換えられない
hash = { b: 'B', c: 'C' }
p "abcabc".gsub(/[bc]/, hash)     #=> "aa"

# キー が Symbol の Hash は String に変換する必要がある
p "abcabc".gsub(/[bc]/, hash.transform_keys(&:to_s))     #=> "aBCaBC"
```


## 提案する動作

```ruby
# キーが String の場合は現行維持
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"

hash = { b: 'B', c: 'C' }

# $& は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){hash[$&]} #=> "aa"

# ブロックの引数は動的であるべきなので String のまま
p "abcabc".gsub(/[bc]/){ |s| hash[s] } #=> a"

# Hash を直接渡した場合のみキーが Symbol でも許容する
p "abcabc".gsub(/[bc]/, hash)     #=> "aBCaBC"
```


## 利点

* キーを Symbol で書くことを推奨しているコーディング規約がある
  * [ruby-style-guide/README.ja.md at japanese · fortissimo1997/ruby-style-guide · GitHub](https://github.com/fortissimo1997/ruby-style-guide/blob/japanese/README.ja.md#symbols-as-keys)
* キーを Symbol で定義する方が Hash を書いていて気持ちがいい


## 課題

* `String` と `Symbol` の両方のキーがあった場合どうするか
  * `"abcabc".sub(/[bc]/, { "b" => "A", b: "C" })  # => ???`
  * 現状は `String` を優先している
  * それ以前に `String` と `Symbol` が混ざっている Hash はおかしいのではないだろうか
  * 警告を出すとか?


## `String#gsub` のユースケースなど

```ruby
# http://batsov.com/articles/2013/10/03/using-rubys-gsub-with-a-hash/
def geekify(string)
  string.gsub(/[leto]/, l: '1', e: '3', t: '7', o: '0')
end

p geekify('leet') # => '1337'
p geekify('noob') # => 'n00b'


def doctorize(string)
  string.gsub(/M(iste)?r/, Mister: 'Doctor', Mr: 'Dr')
end

p doctorize('Mister Freeze') # => 'Doctor Freeze'
p doctorize('Mr Smith')   # => 'Dr Smith'
```

```ruby
# https://coderwall.com/p/t4y7cw/ruby-gsub-with-a-hash-or-block
amino_acid_hash = { A: 'Ala', R: 'Arg', N: 'Asn' }

p "R232A".gsub(/[A-Z]/, amino_acid_hash)
# => "Arg232Ala"
```

```ruby
# https://qiita.com/scivola/items/416155c307ec29a37b8f
hash = {
  '&': "&amp",
  '<': "&lt",
  '>': "&gt",
}

p "<Q&A>".gsub(/[&<>]/, hash)
# => "&ltQ&ampA&gt"
```

```ruby
# https://qiita.com/pocari/items/34855a9b07ea5006fe80
hash = {
  '#to#': "taro",
  '#from#': "jiro",
}

template = <<EOS
hello, #to#.
message from #from#.
EOS

puts template.gsub(/#.*#/, hash)
# => hello, taro.
# message from jiro.
```

その他、具体的なユースケースを思いついた方がいればコメントいただけると助かります。

---Files--------------------------------
string_sub_with_symbol_key.patch (2.71 KB)


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2018-02-04 13:05 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-14417.20180129093502@ruby-lang.org>
2018-01-29  9:35 ` [ruby-dev:50445] [Ruby trunk Feature#14417] String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案 manga.osyo
2018-01-29  9:56 ` [ruby-dev:50446] " hanmac
2018-01-29  9:59 ` [ruby-dev:50447] [Ruby trunk Feature#14417][Feedback] " shyouhei
2018-01-29 10:51 ` [ruby-dev:50448] [Ruby trunk Feature#14417] " manga.osyo
2018-01-29 15:17 ` [ruby-dev:50449] " hanmac
2018-01-31 11:06 ` [ruby-dev:50454] " duerst
2018-01-31 11:17 ` [ruby-dev:50455] " hanmac
2018-02-04 13:05 ` [ruby-dev:50467] " naruse

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).