How to replace the contents of a string in Ruby
For this example we will take the string "bacon, eggs"
and using that string create a new string, "bacon, orange juice"
, while leaving the original string intact.
To accomplish this task we will use the String
class method gsub
. In the most basic use case we will pass two arguments gsub
:
- The contents of the string we want to replace.
- The replacement text.
The result will return a new string with the contents replaced:
bacon_and_eggs = "bacon, eggs"
bacon_and_oj = bacon_and_eggs.gsub("eggs", "orange juice")
=> bacon_and_eggs
"bacon, eggs"
=> bacon_and_oj
"bacon, orange juice"
In more advanced use cases you can pass a pattern you want to match as the first argument to gsub
.
Further reading: