To replace a substring in a string in Ruby, you can use the `gsub()` method or regular expressions.
In this example,
str
with a value of "Hello World"
.gsub()
method to replace the substring "World"
with "Universe"
.str
.str = "Hello World"
str.gsub!(/World/, 'Universe')
puts str
Hello Universe
In this tutorial, we learned How to Replace a Substring in String in Ruby language with well detailed examples.