Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Dumb str.sub question

Thread Tools

Dumb str.sub question

Orion Hunter
Posts: n/a
I feel really dumb asking this, but I just can't seem to figure it out.

I am trying to parse some text for use in a mysql insert statement. That
being the case, I need to escape out quote characters.

If str= "isn't stands for is not", what I want is a string with "isn\'t
stands for is not"

At first I thought, sure, no problem!! But I can't get it. I've tried the

irb:1> str = "isn't stands for is not"
irb:2> str.sub( '\'', '\'' ) --> "isn't stands for is not" (expected)
irb:3> str.sub( '\'', '\\'' ) --> unterminated string (expected)
irb:4> str.sub( '\'', '\\\' ) --> "isnt stands for is nott stands for is
not" (huh? Why this wierd double take?)
irb:5> str.sub( '\'' ){ |m| m = "\'" } --> "isn't stands for is not"
irb:6> str.sub( '\'' ){ |m| m = "\\'" } --> "isn\\'t stands for is not" (why
did it insert TWO \'s?, and not just one? I would have exected the first one
to "escape" the second, thus giving \' as desired)

So, my quesitons are:
1) How do I do my escape character substituion
2) Why did #4 and #6 behave as they did?


__________________________________________________ _______________
Gift-shop online from the comfort of home at MSN Shopping! No crowds, free

Reply With Quote
Mike Stok
Posts: n/a
In article <(E-Mail Removed)>,
Mark J. Reed <(E-Mail Removed)> wrote:

> irb(main):006:0> str.sub(/'/) { '\\\'' }
> => "isn\\'t stands for is not"

Sometimes it is useful to pick your quotes carefully e.g.

str.sub(/'/) { '\\\'' }

as you're quoting a single quote then using double quotes saves a \

str.sub(/'/) { "\\'" }


-- Removed) | The "`Stok' disclaimers" apply. | GPG PGP Key 1024D/059913DA
(E-Mail Removed) | Fingerprint 0570 71CD 6790 7C28 3D60 | 75D2 9EC4 C1C0 0599 13DA
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dumb, Dumb Vista Au79 Computer Support 4 02-11-2007 03:40 PM
Probably a dumb s/// question. Mark Healey Perl 2 03-16-2005 04:51 PM
Dumb, dumb dumb Qestion David Napierkowski Digital Photography 6 10-31-2004 11:14 PM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM
Re: Dumb question Walter Roberson Cisco 1 07-23-2003 01:05 PM