Forum: Ruby addslashes()

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.
Ef53c4abeae8cc46327030bae489a33c?d=identicon&s=25 Fred Phillips (Guest)
on 2007-06-25 18:18
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Is there a built-in function in Ruby similar to PHP's addslashes()
function?
MySQL keeps breaking...
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFGf+p/2vy7v+d+psQRAqeDAKCLHdMotVW85fMMrBTKAezUHxQdRACfXsDb
gB5LfblLO7n3Gy+1kgo8mI4=
=YLAI
-----END PGP SIGNATURE-----
Cf6d0868b2b4c69bac3e6f265a32b6a7?d=identicon&s=25 Daniel Martin (Guest)
on 2007-06-25 21:15
(Received via mailing list)
"Fred Phillips" <fophillips1990@gmail.com> writes:

> Is there a built-in function in Ruby similar to PHP's addslashes() function?
> MySQL keeps breaking...

Well, this probably indicates that you're doing sql by dynamically
building statements, which is generally a frowned-upon practice in
favor of using prepared statements with ? placeholders.  However, if
you're absolutely determined to do this:

def addslashes(str)
  str.gsub(/['"\\\x0]/,'\\\\\0')
end

def stripslashes(str)
  str.gsub(/\\(.)/,'\1')
end
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-06-25 21:19
(Received via mailing list)
Fred Phillips wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Is there a built-in function in Ruby similar to PHP's addslashes()
> function?
> MySQL keeps breaking...

There should be a #escape_str or #quote method on the connection object
that will do what you're after.
This topic is locked and can not be replied to.