Forum: Ruby on Rails Backslash Escaping in View

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F2a8e4cc069558b818508c80052f2f5e?d=identicon&s=25 Brent Johnson (Guest)
on 2006-03-31 21:57
(Received via mailing list)
Ok, I'm hoping this won't be a forehead slapper, but take a look at this
code:

 physician.name.sub(/'/, "\\'")

I'm trying to substitute a single quote with a backslash and a single
quote.  This seems pretty simple right?  Well, someone kick me and
tell me why it's not :)

Here's what I'm getting if the physician's name is Test's User:

Tests Users User

Huh?  Is this normal Ruby behavior (meaning I shoulda sent this to the
Ruby group) or is it something funky with the View code? (which is why
I sent this here first)

Thanks,

- Brent
5b132632f47e8c7e2a83157cef4649c8?d=identicon&s=25 Agnieszka Figiel (agnessa)
on 2006-03-31 22:01
Brent Johnson wrote:
> Ok, I'm hoping this won't be a forehead slapper, but take a look at this
> code:
>
>  physician.name.sub(/'/, "\\'")
>
> I'm trying to substitute a single quote with a backslash and a single
> quote.  This seems pretty simple right?  Well, someone kick me and
> tell me why it's not :)
>
> Here's what I'm getting if the physician's name is Test's User:
>
> Tests Users User
>
> Huh?  Is this normal Ruby behavior (meaning I shoulda sent this to the
> Ruby group) or is it something funky with the View code? (which is why
> I sent this here first)
>
> Thanks,
>
> - Brent

you neeed to replace it with "\\\\'"

have a look here:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/...

and in the documentation for sub

--
Agnieszka
F2a8e4cc069558b818508c80052f2f5e?d=identicon&s=25 Brent Johnson (Guest)
on 2006-03-31 22:27
(Received via mailing list)
Ahhh, I should have thought to search for "backreference" instead of
just backslash.  I did look at the API reference for the String object
but it didn't mention the quadruple backslash.

Thanks for the info!

- Brent
This topic is locked and can not be replied to.