Skip to content

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

instance method

String#sub!

(::Regexp | ::string pattern, ::string | ::hash[::String, ::_ToS] replacement) -> self?
(::Regexp | ::string pattern) { (::String match) -> ::_ToS } -> self?
sub!(pattern, replacement) → self or nil
sub!(pattern) {|match| ... } → self or nil

Replaces the first occurrence (not all occurrences) of the given pattern on self; returns self if a replacement occurred, nil otherwise.

See Substitution Methods.

Related: String#sub, String#gsub, String#gsub!.

See also #sub #succ #strip! #succ! also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0