Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

String#replace

(::string other_string) -> self
replace(other_string) → self

Replaces the contents of self with the contents of other_string; returns self:

Example
s = 'foo'        # => "foo"
s.replace('bar') # => "bar"

Related: see Modifying.

See also #prepend #reverse #partition #reverse! also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0