Mithun Sasidharan Mithun Sasidharan - 7 months ago 36
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??


You can try using this way :

sentence ["Robert"] = "Roger"

Then the sentence will become :

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