Ruby GSUB question

Hello All,

In Ruby if I have the string:

<a href="#" onclick="new
Ajax.Request('/accounts/todo_lists/1/todo_items/2', {asynchronous:true,
evalScripts:true, parameters:'authenticity_token=' +
encodeURIComponent('0523c2646f11e096ef9ecd2f6e5b41690f99fc39')}); return
false;">Remove</a>

How would I go about replacing the single quotes ', with a backslash and
single quote? \'

Ultimately I'd like to arrive at the string below:

<a href="#" onclick="new
Ajax.Request(\'/accounts/todo_lists/1/todo_items/2\',
{asynchronous:true, evalScripts:true, parameters:\'authenticity_token=\'
+ encodeURIComponent(\'0523c2646f11e096ef9ecd2f6e5b41690f99fc39\')});
return false;">Remove</a>

Every variation of GSUB that I've tried has yet to yield that second
string. Any assistance would be greatly appreciated.

Thank you,

Binh

···

--
Posted via http://www.ruby-forum.com/.

# <a href="#" onclick="new
# Ajax.Request('/accounts/todo_lists/1/todo_items/2',
# {asynchronous:true,
# evalScripts:true, parameters:'authenticity_token=' +
# encodeURIComponent('0523c2646f11e096ef9ecd2f6e5b41690f99fc39')
# }); return
# false;">Remove</a>
# How would I go about replacing the single quotes ', with a
# backslash and
# single quote? \'

you can try harder :wink:

puts "c'mon, it's cool!".gsub("'","\\\\'")
c\'mon, it\'s cool!
#=> nil

or simply just,

puts "c'mon, it's cool!".gsub("'"){"\\'"}
c\'mon, it\'s cool!

···

From: Binh Ly [mailto:binh@pigbaby.net]

I understand the first \\ , as it will produce \ when it's displayed ,
but why are there still 2 more \\ ? As I see it , \\\\' would produce
\\' , and only when it will be printed will we see \' . Could someone
please explain this ?

···

--
Posted via http://www.ruby-forum.com/.

Lex Williams wrote:

I understand the first \\ , as it will produce \ when it's displayed ,
but why are there still 2 more \\ ? As I see it , \\\\' would produce
\\' , and only when it will be printed will we see \' . Could someone
please explain this ?

Inside a single- or double-quoted string, \ is an escape character, and
to get a literal backslash you have to escape it with another backslash.
So "\\" is a single backslash; "\\\\" is two backslashes.

irb(main):002:0> "\\".size
=> 1

Inside a regexp replacement string, backslash is also an escape
character, because (for example) \1 means "substitute the first capture
group", i.e. the string inside the first set of parentheses.

irb(main):006:0> "abc123def".gsub(/(\d+)/, '*\\1*')
=> "abc*123*def"

So to actually substitute a backslash, the replacement string has to
contain backslash backslash, and you need to write that as "\\\\" or
'\\\\'

irb(main):007:0> "abc123def".gsub(/\d+/, '\\\\')
=> "abc\\def"
       ^
   this is a single backslash in the replacement

If using the block form of gsub then there is no need for the \1 syntax,
since there are other ways of achieving the same thing, and so the
second level of backslash escaping is disabled.

irb(main):009:0> "abc123def".gsub(/(\d+)/) { "*#{$1}*" }
=> "abc*123*def"
irb(main):010:0> "abc123def".gsub(/\d+/) { '\\' }
=> "abc\\def"

Of course, if all this is for escaping SQL, whatever DB access library
you use likely has existing methods to do this for you. e.g. for
ActiveRecord:

n = "o'reilly"
Person.find(:all, :conditions => ["name=?", n])

HTH,

Brian.

···

--
Posted via http://www.ruby-forum.com/\.

#...
# Inside a single- or double-quoted string, \ is an escape
# character, and
# to get a literal backslash you have to escape it with another
# backslash.
# So "\\" is a single backslash; "\\\\" is two backslashes.
#...
# <snipped enlightening message>

otoh, perl also has regex power tools like \Q..\E eg
(escaping without using the backslash ie)

maybe we could have something similar?

=)

kind regards -botp

···

From: Brian Candler [mailto:b.candler@pobox.com]

Peña, Botp wrote:

otoh, perl also has regex power tools like \Q..\E eg
(escaping without using the backslash ie)

maybe we could have something similar?

=)

Interesting; I wasn't aware those worked inside the replacement string,
as well as the regexp.

However, I'm not sure I'd want to copy Perl's ugliness here. From 'man
perlre':

       You cannot include a literal "$" or "@" within a "\Q" sequence.
An
       unescaped "$" or "@" interpolates the corresponding variable,
while
       escaping will cause the literal string "\$" to be matched.
You’ll need
       to write something like "m/\Quser\E\@\Qhost/".
...
       Beware that if you put literal backslashes (those not inside
interpo‐
       lated variables) between "\Q" and "\E", double-quotish backslash
inter‐
       polation may lead to confusing results. If you need to use
literal
       backslashes within "\Q...\E", consult "Gory details of parsing
quoted
       constructs" in perlop.

That is, if you write s/'/\Q\'\E/ you'd better know what you're doing.

I think I'll live with /#{Regexp.escape("....")}/ instead :slight_smile:

B.

···

--
Posted via http://www.ruby-forum.com/\.

# ..enlightening talk..
# I think I'll live with /#{Regexp.escape("....")}/ instead :slight_smile:

you're right. ' totally forgot about regexp#escape as another solution to the op. it is much better.

kind regards -botp

···

From: Brian Candler [mailto:b.candler@pobox.com]