Project

General

Profile

Actions

Feature #22238

open

String#tr to take a Hash for multi-character replacements

Feature #22238: String#tr to take a Hash for multi-character replacements
1

Added by byroot (Jean Boussier) 4 days ago. Updated 2 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:126356]

Description

This is @matz's counter proposal to [Feature #22229] from the last dev-meeting

matz: I will counter-propose String#tr-with-hash style.

'Hello </script>'.tr(">" => '\u003e', "<" => '\u003c', "&" => '\u0026') # matz: OK

'Hello </script>'.tr("abc" => 'ABC') # should raise an exception
'Hello </script>'.tr("abc" => 'ABC', "ab" => "XY") # should raise an exception

"fée".tr("é" => "€") # it should work

"Hello".tr("l" => "ABC", "o" => "XYZ") #=> "HeABCABCXYZ"

"Hello".gsub(/./m) { hash[$&] || $& } # gsub equivalent

Notes:

  • Hash keys MUST be single characters (but can be multi-byte).
  • Values can be multiple characters.

Related issues 1 (0 open1 closed)

Related to Ruby - Feature #22229: Allow GCI.escapeHTML to take a custom escape tableRejectedActions
Actions

Also available in: PDF Atom