Question: gsub allowing double quotations inside gsub


gsub allowing double quotations inside gsub

Answers 1
Added at 2016-12-29 21:12

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?

nr: #1 dodano: 2016-12-29 21:12

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.

Source Show
◀ Wstecz