End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
String#sub
sub(pattern, replacement) → new_string
sub(pattern) {|match| ... } → new_string
string.c
View on GitHub ↗
static VALUE
rb_str_sub(int argc, VALUE *argv, VALUE str)
{
str = str_duplicate(rb_cString, str);
rb_str_sub_bang(argc, argv, str);
return str;
}
Returns a copy of self with only the first occurrence (not all occurrences) of the given pattern replaced.
See Substitution Methods.
Related: String#sub!, String#gsub, String#gsub!.
Ruby version changes
-
3.0
-
The following methods now return or yield
Stringinstances instead of subclass instances when called on subclass instances: [Bug #10845]
-