Dumb str.sub question

Discussion in 'Ruby' started by Orion Hunter, Nov 26, 2003.

  1. Orion Hunter

    Orion Hunter Guest

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

    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"
    (expected)
    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?

    Thanks.

    _________________________________________________________________
    Gift-shop online from the comfort of home at MSN Shopping! No crowds, free
    parking. http://shopping.msn.com
    Orion Hunter, Nov 26, 2003
    #1
    1. Advertising

  2. Orion Hunter

    Mike Stok Guest

    In article <>,
    Mark J. Reed <> 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(/'/) { "\\'" }

    Mike

    --
    | The "`Stok' disclaimers" apply.
    http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
    | Fingerprint 0570 71CD 6790 7C28 3D60
    http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA
    Mike Stok, Nov 26, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    2
    Views:
    470
    Thomas G. Marshall
    Aug 3, 2003
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    616
    CBFalconer
    Apr 10, 2004
  3. Ben
    Replies:
    2
    Views:
    866
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    1,979
    Lawrence D'Oliveiro
    May 20, 2011
  5. Jerry C.
    Replies:
    8
    Views:
    214
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page