Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

String#tr_s

(::String::selector source, ::string replacement) -> ::String
tr_s(selector, replacements) → new_string

Like String#tr, except:

  • Also squeezes the modified portions of the translated string; see String#squeeze.

  • Returns the translated and squeezed string.

Examples:

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

Related: see Converting to New String.

Ruby version changes

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