Project

General

Profile

Actions

Feature #14417

closed

String#sub / String#gsub に『キーが Symbol の Hash』を渡せるようにする提案

Added by osyo (manga osyo) about 6 years ago. Updated about 6 years ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-dev:50445]

Description

概要

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

現行の動作

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"

提案する動作

# キーが 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"

利点

課題

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

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

# 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'
# 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"
# https://qiita.com/scivola/items/416155c307ec29a37b8f
hash = {
  '&': "&amp",
  '<': "&lt",
  '>': "&gt",
}

p "<Q&A>".gsub(/[&<>]/, hash)
# => "&ltQ&ampA&gt"
# 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) string_sub_with_symbol_key.patch osyo (manga osyo), 01/29/2018 09:34 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0