Mithun Sasidharan Mithun Sasidharan - 2 months ago 5
Ruby Question

Replace words in string - ruby

I have a string in ruby

sentence = "My name is Robert"


How can i replace any one word in this sentence easily without using complex code or loop??

Answer

You can try using this way :

sentence ["Robert"] = "Roger"

Then the sentence will become :

sentence = "My name is Roger" # Robert is replaced with Roger
Comments