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.
Brent J. (Guest)
on 2006-03-31 23: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
Agnieszka F. (Guest)
on 2006-04-01 00:01
Brent J. 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
Brent J. (Guest)
on 2006-04-01 00: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.