[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby GSUB question

Binh Ly

9/22/2008 6:24:00 AM

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-....

12 Answers

Peña, Botp

9/22/2008 7:04:00 AM

0

RnJvbTogQmluaCBMeSBbbWFpbHRvOmJpbmhAcGlnYmFieS5uZXRdIA0KIyA8YSBocmVmPSIjIiBv
bmNsaWNrPSJuZXcNCiMgQWpheC5SZXF1ZXN0KCcvYWNjb3VudHMvdG9kb19saXN0cy8xL3RvZG9f
aXRlbXMvMicsIA0KIyB7YXN5bmNocm9ub3VzOnRydWUsDQojIGV2YWxTY3JpcHRzOnRydWUsIHBh
cmFtZXRlcnM6J2F1dGhlbnRpY2l0eV90b2tlbj0nICsNCiMgZW5jb2RlVVJJQ29tcG9uZW50KCcw
NTIzYzI2NDZmMTFlMDk2ZWY5ZWNkMmY2ZTViNDE2OTBmOTlmYzM5JykNCiMgfSk7IHJldHVybg0K
IyBmYWxzZTsiPlJlbW92ZTwvYT4NCiMgSG93IHdvdWxkIEkgZ28gYWJvdXQgcmVwbGFjaW5nIHRo
ZSBzaW5nbGUgcXVvdGVzICcsIHdpdGggYSANCiMgYmFja3NsYXNoIGFuZA0KIyBzaW5nbGUgcXVv
dGU/IFwnDQoNCnlvdSBjYW4gdHJ5IGhhcmRlciA7KQ0KDQpwdXRzICJjJ21vbiwgaXQncyBjb29s
ISIuZ3N1YigiJyIsIlxcXFwnIikNCmNcJ21vbiwgaXRcJ3MgY29vbCENCiM9PiBuaWwNCg0Kb3Ig
c2ltcGx5IGp1c3QsDQoNCnB1dHMgImMnbW9uLCBpdCdzIGNvb2whIi5nc3ViKCInIil7IlxcJyJ9
DQpjXCdtb24sIGl0XCdzIGNvb2whDQoNCg0K

Lex Williams

9/22/2008 7:09:00 AM

0

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-....

Brian Candler

9/22/2008 7:38:00 AM

0

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-....

Peña, Botp

9/22/2008 8:13:00 AM

0

RnJvbTogQnJpYW4gQ2FuZGxlciBbbWFpbHRvOmIuY2FuZGxlckBwb2JveC5jb21dIA0KIy4uLg0K
IyBJbnNpZGUgYSBzaW5nbGUtIG9yIGRvdWJsZS1xdW90ZWQgc3RyaW5nLCBcIGlzIGFuIGVzY2Fw
ZSANCiMgY2hhcmFjdGVyLCBhbmQgDQojIHRvIGdldCBhIGxpdGVyYWwgYmFja3NsYXNoIHlvdSBo
YXZlIHRvIGVzY2FwZSBpdCB3aXRoIGFub3RoZXIgDQojIGJhY2tzbGFzaC4gDQojIFNvICJcXCIg
aXMgYSBzaW5nbGUgYmFja3NsYXNoOyAiXFxcXCIgaXMgdHdvIGJhY2tzbGFzaGVzLg0KIy4uLg0K
IyA8c25pcHBlZCBlbmxpZ2h0ZW5pbmcgbWVzc2FnZT4NCg0KDQpvdG9oLCBwZXJsIGFsc28gaGFz
IHJlZ2V4IHBvd2VyIHRvb2xzIGxpa2UgXFEuLlxFIGVnDQooZXNjYXBpbmcgd2l0aG91dCB1c2lu
ZyB0aGUgYmFja3NsYXNoIGllKQ0KDQptYXliZSB3ZSBjb3VsZCBoYXZlIHNvbWV0aGluZyBzaW1p
bGFyPw0KDQo9KQ0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==

Brian Candler

9/22/2008 9:06:00 AM

0

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 :-)

B.
--
Posted via http://www.ruby-....

Peña, Botp

9/22/2008 9:17:00 AM

0

RnJvbTogQnJpYW4gQ2FuZGxlciBbbWFpbHRvOmIuY2FuZGxlckBwb2JveC5jb21dIA0KIyAuLmVu
bGlnaHRlbmluZyB0YWxrLi4NCiMgSSB0aGluayBJJ2xsIGxpdmUgd2l0aCAvI3tSZWdleHAuZXNj
YXBlKCIuLi4uIil9LyBpbnN0ZWFkIDotKQ0KDQp5b3UncmUgcmlnaHQuICcgdG90YWxseSBmb3Jn
b3QgYWJvdXQgcmVnZXhwI2VzY2FwZSBhcyBhbm90aGVyIHNvbHV0aW9uIHRvIHRoZSBvcC4gaXQg
aXMgbXVjaCBiZXR0ZXIuDQoNCmtpbmQgcmVnYXJkcyAtYm90cA0KDQoNCg==

AlleyCat

12/24/2012 7:28:00 AM

0

On Sun, 23 Dec 2012 20:40:42 -0700, In article
<7ljfd8teegeeqkhv9d4nb7l8bij5ar8gcl@4ax.com>, Yoorghis@Jurgis.net
says...

> Right now---it's gunloons and weirdos.

They're YOUR gun loons and weirdos. YOUR gun loon monkeys kill more
people than ANYONE!

http://static2.bigstockphoto.com/thumbs/5/1/4/large2/4...

http://image.shutterstock.com/display_pic_with_logo/53366/5336...
62,4/stock-photo-gang-member-in-a-bullet-proof-vest-841133.jpg

http://static5.depositphotos.com/1008070/403/i/950/depo...
4039985-Gang-Banger.jpg

http://image.shutterstock.com/display_pic_with_logo/1064/1064,...
,2/stock-photo-gang-banger-with-gun-22121719.jpg

http://www.youtube.com/watch?v=9...
http://www.youtube.com/watch?v=7...
http://www.youtube.com/watch?v=M...



--
Check for Children Redirecting Newsgroups

AlleyCat

12/24/2012 7:43:00 PM

0

On Mon, 24 Dec 2012 09:54:26 -0700, In article
<352hd8177njo7l6v4qkkvltq796d30anuu@4ax.com>, Yoorghis@Jurgis.net
says...
>
> On Mon, 24 Dec 2012 01:28:05 -0600, AlleyCat <alley@aohell.com> wrote:
>
> >On Sun, 23 Dec 2012 20:40:42 -0700, In article
> ><7ljfd8teegeeqkhv9d4nb7l8bij5ar8gcl@4ax.com>, Yoorghis@Jurgis.net
> >says...
> >
> >> Right now---it's gunloons and weirdos.
> >
> >They're YOUR gun loons and weirdos.
>
> Not so.
>
> Perhaps you need to walk around the Mt's of Montana
>
> See how long you last.

What the fuck does that mean? I know what it would mean if you walked
into any black neighborhood in Chicago. Montana? What the fuck does that
mean? You need to be turned in... you're crazy. You live in another
fucking world... get help, and stay away from mommy's guns.

--
Check for Children Redirecting Newsgroups

talk.politics.misc,alt.fan.rush-
limbaugh,alt.politics.economics,alt.politics,alt.politics.usa

Yoorghis

12/24/2012 8:53:00 PM

0

On Mon, 24 Dec 2012 13:42:58 -0600, AlleyCat <alley@aohell.com> wrote:

>On Mon, 24 Dec 2012 09:54:26 -0700, In article
><352hd8177njo7l6v4qkkvltq796d30anuu@4ax.com>, Yoorghis@Jurgis.net
>says...
>>
>> On Mon, 24 Dec 2012 01:28:05 -0600, AlleyCat <alley@aohell.com> wrote:
>>
>> >On Sun, 23 Dec 2012 20:40:42 -0700, In article
>> ><7ljfd8teegeeqkhv9d4nb7l8bij5ar8gcl@4ax.com>, Yoorghis@Jurgis.net
>> >says...
>> >
>> >> Right now---it's gunloons and weirdos.
>> >
>> >They're YOUR gun loons and weirdos.
>>
>> Not so.
>>
>> Perhaps you need to walk around the Mt's of Montana
>>
>> See how long you last.
>
>What the fuck does that mean? I know what it would mean if you walked
>into any black neighborhood in Chicago. Montana? What the fuck does that
>mean?

see---you got NO idea who, or where the REAL Crazies are, do you.

You walk out in montana and the likelihood of being shot by one of the
white supremacists, the anarchists, the pot growers, the doomsday
idiots, is almost certain.

You got no idea.

AlleyCat

12/25/2012 7:19:00 AM

0

on, 24 Dec 2012 13:52:39 -0700, In article
<k1ghd8pkpkmggm6a200oh2dtcg1f4jb4dq@4ax.com>, Yoorghis@Jurgis.net
says...


> You walk out in montana and the likelihood of being shot by one of the
> white supremacists, the anarchists, the pot growers, the doomsday
> idiots, is almost certain.

Bullshit... please cite your facts. If YOU walk onto ANY of these
people's lands, YOU probably WILL be shot. You look like this, and
you're dead.

http://fc06.deviantart.net/fs70/i/2010/027/c/5/Basement_Troll_...
ANTIS3YE.jpg

You forgot ALL the inner cities, where you will be shot, just for being
white. These "people" are America's mass murderers. They've killed
1000's more than some crazy guys... ever:

http://static2.bigstockphoto.com/thumbs/5/1/4/large2/4...

http://image.shutterstock.com/display_pic_with_logo/53366/5336...
62,4/stock-photo-gang-member-in-a-bullet-proof-vest-841133.jpg

http://static5.depositphotos.com/1008070/403/i/950/depo...
4039985-Gang-Banger.jpg

http://image.shutterstock.com/display_pic_with_logo/1064/1064,...
,2/stock-photo-gang-banger-with-gun-22121719.jpg

http://www.youtube.com/watch?v=9...
http://www.youtube.com/watch?v=7...
http://www.youtube.com/watch?v=M...

Don't rationalize their animalistic behavior with pathetic attempts to
demonize white supremacists or survivalists. Niggers kill far more
people. Go on... cite your rubbish. You can't, can you, pussy?
Montana... LOL... yeah I see murders in Montana on the news, daily. What
a fucking moron. Can't argue the libtard point, so you make shit up....
stupid fucking lies.

--
Check for Children Redirecting Newsgroups