From: "byroot (Jean Boussier) via ruby-core" Date: 2026-08-10T06:53:24+00:00 Subject: [ruby-core:126356] [Ruby Feature#22238] String#tr to take a Hash for multi-character replacements Issue #22238 has been reported by byroot (Jean Boussier). ---------------------------------------- Feature #22238: String#tr to take a Hash for multi-character replacements https://bugs.ruby-lang.org/issues/22238 * Author: byroot (Jean Boussier) * Status: Open ---------------------------------------- This is @matz's counter proposal to [Feature #22229] from the last dev-meeting > matz: I will counter-propose String#tr-with-hash style. ```ruby 'Hello '.tr(">" => '\u003e', "<" => '\u003c', "&" => '\u0026') # matz: OK 'Hello '.tr("abc" => 'ABC') # should raise an exception 'Hello '.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. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/