Can't seem to figure out how to secure the '\' character

In my Rails application, the user’s username basically has a dir created
for it… For that reason, I have to make sure it doesn’t contain \
characters. (among others). I have been successful in filtering out the
others with the include? method, but when I do: if sURL.include? “”,
there is an error… How would I ask if the var sURL contained the \
character? Do I have to do any escaping? thanks.

Ben wrote:

when I do: if sURL.include? “”, there is an error… How would I ask
if the var sURL contained the \ character? Do I have to do any
escaping? thanks.

Yes, you’re basically escaping the quotation mark now. Do
sURL.include?("\") instead.


Jakob S. - http://mentalized.net

On Mon, 2 Oct 2006 15:07:10 +0200
Ben [email protected] wrote:

In my Rails application, the user’s username basically has a dir created
for it… For that reason, I have to make sure it doesn’t contain \
characters. (among others). I have been successful in filtering out the
others with the include? method, but when I do: if sURL.include? “”,
there is an error… How would I ask if the var sURL contained the \
character? Do I have to do any escaping? thanks.

Hey Ben, what you actually want to do is the inverse. You want a regex
that has only allowed characters and then you want to reject anything
else. In other words, you have “I accept everything except, …” which
leads to you playing constant catch-up. What you really want is “I
reject everything, except …” or “I accept only X and reject
everything else.”

I would start with something like this:

if username =~ /^[a-zA-Z0-9]*$/

accepted!

else

rejected!

end

Another thing is to strip the username of spaces and then freeze it so
that it doesn’t get double interpreted or accidentally modified later in
your program.


Zed A. Shaw, MUDCRAP-CE Master Black Belt Sifu


http://mongrel.rubyforge.org/
http://www.lingr.com/room/3yXhqKbfPy8 – Come get help.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs