Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

String#tr_s

str.tr_s(from_str, to_str) → new_str

Processes a copy of str as described under String#tr, then removes duplicate characters in regions that were affected by the translation.

Example
"hello".tr_s('l', 'r')     #=> "hero"
"hello".tr_s('el', '*')    #=> "h*o"
"hello".tr_s('el', 'hx')   #=> "hhxo"

Ruby version changes

  1. 3.0
See also #tr_s! #tr! #tr #undump also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0