Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

String#tr_s

tr_s(selector, replacements) → string

Like String#tr, but also squeezes the modified portions of the translated string; returns a new string (translated and squeezed).

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

Related: String#squeeze.

Ruby version changes

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