From: "luke-gru (Luke Gruber) via ruby-core" Date: 2026-08-11T13:08:05+00:00 Subject: [ruby-core:126367] [Ruby Feature#22238] String#tr to take a Hash for multi-character replacements Issue #22238 has been updated by luke-gru (Luke Gruber). Status changed from Closed to Open The commit in the previous message has the wrong Issue ID, it is unrelated. ---------------------------------------- Feature #22238: String#tr to take a Hash for multi-character replacements https://bugs.ruby-lang.org/issues/22238#change-118506 * 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/