Skip to content

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

instance method

String#gsub

(::Regexp | ::string pattern, ::string | ::hash[::String, ::_ToS] replacement) -> ::String
(::Regexp | ::string pattern) -> ::Enumerator[::String, ::String]
(::Regexp | ::string pattern) { (::String match) -> ::_ToS } -> ::String
gsub(pattern, replacement) → new_string
gsub(pattern) {|match| ... } → new_string
gsub(pattern) → enumerator

Returns a copy of self with all occurrences of the given pattern replaced.

See Substitution Methods.

Returns an Enumerator if no replacement and no block given.

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

Ruby version changes

  1. 3.0
See also #gsub! #grapheme_clusters #getbyte #hash also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0