Ruby GSUB question

Hello All,

In Ruby if I have the string:

Remove

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

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

Thank you,

Binh

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 ?

From: Binh Ly [mailto:[email protected]]

<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

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!

Lex W. 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’)
=> “abc123def”

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}” }
=> “abc123def”
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.

From: Brian C. [mailto:[email protected]]
#…

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.

#…

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

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.

From: Brian C. [mailto:[email protected]]

…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