Skip to content

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

instance method

String#gsub

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.4 · 3.2 · 3.1 · 3.0