'\\\\\\\\' madness?

Discussion in 'Ruby' started by Fabian Streitel, Sep 7, 2009.

  1. [Note: parts of this message were removed to make it a legal post.]

    hi,
    trying to solve someones mailinglist posted problem, I just created my own.
    Why do 2 backslashes produce the same result as 4 and 6 the same as 8?

    014:0> 'ab'.gsub('a', '\\')
    => "\\b"
    015:0> 'ab'.gsub('a', '\\\\')
    => "\\b"
    016:0> 'ab'.gsub('a', '\\\\\\')
    => "\\\\b"
    017:0> 'ab'.gsub('a', '\\\\\\\\')
    => "\\\\b"
    018:0> RUBY_DESCRIPTION
    => "ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-linux]"

    Doesn't that strike anyone as odd?
    Greetz!
     
    Fabian Streitel, Sep 7, 2009
    #1
    1. Advertising

  2. Fabian Streitel

    Martin Boese Guest

    On Mon, 2009-09-07 at 17:36 +0900, Fabian Streitel wrote:
    > hi,
    > trying to solve someones mailinglist posted problem, I just created my own.
    > Why do 2 backslashes produce the same result as 4 and 6 the same as 8?
    >
    > 014:0> 'ab'.gsub('a', '\\')
    > => "\\b"
    > 015:0> 'ab'.gsub('a', '\\\\')
    > => "\\b"
    > 016:0> 'ab'.gsub('a', '\\\\\\')
    > => "\\\\b"
    > 017:0> 'ab'.gsub('a', '\\\\\\\\')
    > => "\\\\b"
    > 018:0> RUBY_DESCRIPTION
    > => "ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-linux]"
    >
    > Doesn't that strike anyone as odd?
    > Greetz!




    Be careful with gsub, it does a lot of magic. From the documentation:

    If a string is used as the replacement, special variables from the
    match (such as +$&+ and +$1+) cannot be substituted into it, as
    substitution into the string occurs before the pattern match
    starts. However, the sequences +\1+, +\2+, and so on may be used to
    interpolate successive groups in the match.

    To fix, put the replacement into a block:

    irb(main):001:0> s = '\\'
    => "\\"
    irb(main):002:0> s.size
    => 1
    irb(main):003:0> s = s * 10
    => "\\\\\\\\\\\\\\\\\\\\"
    irb(main):004:0> s.size
    => 10
    irb(main):005:0> 'a'.gsub('a', s).size
    => 5
    irb(main):006:0> 'a'.gsub('a') { s }.size
    => 10
    irb(main):007:0> 'a'.gsub('a') { s }
    => "\\\\\\\\\\\\\\\\\\\\"
    irb(main):008:0> 'a'.gsub('a', s)
    => "\\\\\\\\\\"



    Martin
     
    Martin Boese, Sep 7, 2009
    #2
    1. Advertising

  3. 2009/9/7 Fabian Streitel <>:
    > hi,
    > trying to solve someones mailinglist posted problem, I just created my ow=

    n.
    > Why do 2 backslashes produce the same result as 4 and 6 the same as 8?
    >
    > 014:0> 'ab'.gsub('a', '\\')
    > =A0 =A0=3D> "\\b"
    > 015:0> 'ab'.gsub('a', '\\\\')
    > =A0 =A0=3D> "\\b"
    > 016:0> 'ab'.gsub('a', '\\\\\\')
    > =A0 =A0=3D> "\\\\b"
    > 017:0> 'ab'.gsub('a', '\\\\\\\\')
    > =A0 =A0=3D> "\\\\b"
    > 018:0> RUBY_DESCRIPTION
    > =A0 =A0=3D> "ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-linux]"
    >
    > Doesn't that strike anyone as odd?


    Well, not me anyway. :) You can find a lot of threads about this in
    the archives... (hint, hint)

    The short story: you need to keep in mind that there are multiple
    levels of escaping going on which unfortunately use the same character
    ("\") as meta character AND output in IRB is done via #inspect.

    To get a single backslash in a string you need to type two:

    irb(main):001:0> puts '\\'
    \
    =3D> nil

    In order to make gsub use a backslash literally (and not interpret it
    as meta character) you need to have two of them in the string:

    irb(main):002:0> puts '\\\\'
    \\
    =3D> nil

    Now, there are some corner cases: your first example uses a
    replacement string with just a single backslash so it looses its
    metaness (because there is nothing behind it to escape). This is true
    for all cases where there is nothing behind it which can be escaped:

    irb(main):004:0> puts '\1', '\\1'
    \1
    \1
    =3D> nil

    HTH

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Sep 7, 2009
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    >
    > Well, not me anyway. :) You can find a lot of threads about this in
    > the archives... (hint, hint)
    >
    >

    ah, yeah, there was something... :) I guess I'm gonna hand myself a quick
    JFGI here...
    sry...

    well... thanks anyways, I think I get it now. that magic is reaaaally mean.
    Greetz!
     
    Fabian Streitel, Sep 8, 2009
    #4
    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. Todd

    Session Variable Madness

    Todd, Jan 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    437
  2. V. Jenks

    RadioButtonList madness!

    V. Jenks, Apr 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    476
    Ashish M Bhonkiya
    Apr 19, 2004
  3. =?Utf-8?B?QUNhdW50ZXI=?=

    Audio Madness @#!#@

    =?Utf-8?B?QUNhdW50ZXI=?=, Feb 26, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    397
    =?Utf-8?B?QUNhdW50ZXI=?=
    Feb 26, 2005
  4. Mr Newbie

    Session Madness

    Mr Newbie, Nov 12, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    411
    Kevin Spencer
    Nov 13, 2005
  5. Marius Horak

    Madness, I call it madness

    Marius Horak, Jun 4, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    175
    Raterus
    Jun 4, 2004
Loading...

Share This Page