# Magic Quotes in Ruby?

**URL:** <https://www.ruby-forum.com/t/magic-quotes-in-ruby/58903>\
**Category:** Ruby\
**Created:** [April 27, 2006, 1:02am UTC](https://www.ruby-forum.com/t/magic-quotes-in-ruby/58903 "2006-04-27T01:02:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [April 27, 2006, 1:02am UTC](https://www.ruby-forum.com/t/magic-quotes-in-ruby/58903/1 "2006-04-27T01:02:07Z")

</div>

(I thought I posted this question just a bit ago but I can’t find it  
now. Sorry if this is a double-posting)

I have some strings in a CGI script of mine that will sometimes contain  
double-quotation marks (""). These behave normally when I use them  
directly (i.e., puts str will return a string that contains quotes just  
fine), but misbehave when I try to concatenate them with other strings  
(i.e., puts str + “other stuff”).

I’ve seen this sort of “magic quotes” problem in other languages, but  
I’m having a lot of trouble getting this to work. I’ve tried several  
variations on gsub, as well as a couple of other things, but none seem  
to work.

Is there a way to work around this?

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [April 27, 2006, 1:13am UTC](https://www.ruby-forum.com/t/magic-quotes-in-ruby/58903/2 "2006-04-27T01:13:15Z")

</div>

Nathan O. wrote:

> (I thought I posted this question just a bit ago but I can’t find it  
> now. Sorry if this is a double-posting)
> 
> I have some strings in a CGI script of mine that will sometimes contain  
> double-quotation marks (""). These behave normally when I use them  
> directly (i.e., puts str will return a string that contains quotes just  
> fine), but misbehave when I try to concatenate them with other strings  
> (i.e., puts str + “other stuff”).

I’m not seeing this:  
irb(main):004:0\> s = “ab"c”"  
=\> “ab"c”"  
irb(main):005:0\> puts s  
ab"c"  
=\> nil  
irb(main):006:0\> puts s + “other stuff”  
ab"c"other stuff  
=\> nil  
irb(main):007:0\>

> I’ve seen this sort of “magic quotes” problem in other languages, but  
> I’m having a lot of trouble getting this to work. I’ve tried several  
> variations on gsub, as well as a couple of other things, but none seem  
> to work.
> 
> Is there a way to work around this  
> I’m guessing this is really an escaping problem. You’re trying to  
> generate html without doing any escaping? Try viewing the source of  
> your generated page – the quotes are no doubt all there just as you  
> output them, but they’re probably interfering with html attributes.

Luke

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [April 27, 2006, 1:15am UTC](https://www.ruby-forum.com/t/magic-quotes-in-ruby/58903/3 "2006-04-27T01:15:45Z")

</div>

Luke B. wrote:

> I’m guessing this is really an escaping problem. You’re trying to  
> generate html without doing any escaping? Try viewing the source of  
> your generated page – the quotes are no doubt all there just as you  
> output them, but they’re probably interfering with html attributes.

Well, I’m glad you’re paying attention to the obvious things, because I  
don’t seem to!

Yes, it’s an HTML escaping problem. Hmm… this might take a while to  
clear up.

---

<div class="post-metadata">

**Author:** ![nko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/91b2a8/32.png) [@nko](https://www.ruby-forum.com/u/nko)\
**Post date:** [April 27, 2006, 1:25am UTC](https://www.ruby-forum.com/t/magic-quotes-in-ruby/58903/4 "2006-04-27T01:25:33Z")

</div>

Not really - use CGI.escapeHTML(str) and you are all set.
