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
string.c
View on GitHub ↗
static VALUE
rb_str_tr_s(VALUE str, VALUE src, VALUE repl)
{
str = str_duplicate(rb_cString, str);
tr_trans(str, src, repl, 1);
return 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
-
3.0
-
The following methods now return or yield
Stringinstances instead of subclass instances when called on subclass instances: [Bug #10845]
-