Search code examples
rubystringdouble-quotes

Adding quotes to a particular word in string


I have a string that has a value that I want to be in quotes. Following is the text which I currently have:

text = "#{value1} has viewed your #{value2}"

I want value2 to be in quotes. I want:

text = ""#{value1} has viewed your "#{value2}""

How can I implement this? Any help will be appreciated.


Solution

  • text = %Q|#{value1} has viewed your "#{value2}"|
    text = "#{value1} has viewed your \"#{value2}\""
    

    or even:

    text = "#{value1} has viewed your" '"' "#{value2}" '"'
    

    More info.