gsub allowing double quotations inside gsub

170 Views Asked by At

I have this

self.description.gsub! 'iframe', 'iframe id="myid" '

And it outputs this when I save:

iframe id=\"myid\"

But I don't want the backslashes \ before each "

How do I do this?

How do I allow 2 x " inside?

1

There are 1 best solutions below

2
On BEST ANSWER

I don't know if you are running it inside IRB, but sometimes you have to print to see how the string looks like:

tiago@lenovo:~$ irb
2.3.0 :001 > str = "frame"
 => "frame" 
2.3.0 :002 > str.gsub('frame','iframe id="myid"')
 => "iframe id=\"myid\"" 
2.3.0 :003 > puts str.gsub('frame','iframe id="myid"')
iframe id="myid"
 => nil

Note that the returned value of gsub is escaping double quotes but that's because the whole string is wrapped by double quotes, the value printed is different though.