behavior of substitutions with sub

Discussion in 'Ruby' started by Robert Klemme, Nov 4, 2006.

  1. Tammo Tjarks wrote:
    > Hello,
    > I have seen a somehow strange behavior with Strings and the sub-method.
    > Maybe you cen give me an idea if it is a bug and where it comes from.
    > It have seen the problem when I wanted to make an substition with a
    > sub(/<regexp>/,"") instead of (/<regexp/,'').
    >
    > To get the idea a example in irb. I tried it also with version 1.8.5 but
    > have seen it first with 1.8.4:
    >
    > ====================================================
    > irb(main):012:0> tst = "Das war ja nix"
    > => "Das war ja nix"
    > irb(main):013:0> tst2 =tst
    > => "Das war ja nix"
    > irb(main):014:0> tst2.sub(/nix/,"was")
    > => "Das war ja was"
    > irb(main):015:0> tst2.sub(/(n)+ix/,"$1")
    > => "Das war ja $1"
    > irb(main):016:0> tst2.sub(/(n)+ix/,"#{$1}")
    > => "Das war ja n"
    > irb(main):017:0> tst2.sub(/(n)+(i)+(x)+/,"#{$3}#{$2}#{$1}")
    > => "Das war ja n"
    > irb(main):018:0> tst2.sub(/(n)+(i)+(x)+/,'\3\2\1')
    > => "Das war ja xin"
    > irb(main):019:0> tst2.sub(/(n)+(i)+(x)+/,"#{$3}#{$2}#{$1}")
    > => "Das war ja xin"
    >
    > ==================================================================
    >
    > What I do not understand is, that the substitution in the ''-expression with
    > \1, \2 and \3 work without trouble but when I use the normal ""-String and
    > the #{$1}, #{$2} and #{$3}-notation he seems alway to refer to the last
    > regular expression and not the one in sub-method.


    That's easily explained: the string you hand off to sub is interpreted
    before the call happens. But the grouping variables are only set after
    that, i.e. when sub executes.

    The version with \1 etc. works, because method sub does the
    interpretation of the string and replaces \1 with contents of the first
    group etc:

    irb(main):005:0> puts( "\\1" )
    \1
    => nil
    irb(main):006:0> puts( "#{$1}" )

    => nil

    > It is only an example. In the script I have seen it I cicrcumvent the
    > problem by adding an extra regular expression before without using the
    > result. But I think it is a strange behavior.


    Not at all once you understand how this works. It is the general
    mechanism of method arguments being evaluated before the method call
    starts. It's basically the same as:

    irb(main):001:0> def foo(x) puts x end
    => nil
    irb(main):002:0> foo( 1 + 2 )
    3
    => nil

    Kind regards

    robert
     
    Robert Klemme, Nov 4, 2006
    #1
    1. Advertisements

  2. Robert Klemme

    Tammo Tjarks Guest

    Hello,
    I have seen a somehow strange behavior with Strings and the sub-method.
    Maybe you cen give me an idea if it is a bug and where it comes from.
    It have seen the problem when I wanted to make an substition with a
    sub(/<regexp>/,"") instead of (/<regexp/,'').

    To get the idea a example in irb. I tried it also with version 1.8.5 but
    have seen it first with 1.8.4:

    ====================================================
    irb(main):012:0> tst = "Das war ja nix"
    => "Das war ja nix"
    irb(main):013:0> tst2 =tst
    => "Das war ja nix"
    irb(main):014:0> tst2.sub(/nix/,"was")
    => "Das war ja was"
    irb(main):015:0> tst2.sub(/(n)+ix/,"$1")
    => "Das war ja $1"
    irb(main):016:0> tst2.sub(/(n)+ix/,"#{$1}")
    => "Das war ja n"
    irb(main):017:0> tst2.sub(/(n)+(i)+(x)+/,"#{$3}#{$2}#{$1}")
    => "Das war ja n"
    irb(main):018:0> tst2.sub(/(n)+(i)+(x)+/,'\3\2\1')
    => "Das war ja xin"
    irb(main):019:0> tst2.sub(/(n)+(i)+(x)+/,"#{$3}#{$2}#{$1}")
    => "Das war ja xin"

    ==================================================================

    What I do not understand is, that the substitution in the ''-expression with
    \1, \2 and \3 work without trouble but when I use the normal ""-String and
    the #{$1}, #{$2} and #{$3}-notation he seems alway to refer to the last
    regular expression and not the one in sub-method.
    It is only an example. In the script I have seen it I cicrcumvent the
    problem by adding an extra regular expression before without using the
    result. But I think it is a strange behavior.

    Any comments about that.

    Regards,
    Tammo
     
    Tammo Tjarks, Nov 4, 2006
    #2
    1. Advertisements

  3. Robert Klemme

    Tammo Tjarks Guest

    Thanks for you answer. That makes sense.

    Regards,
    Tammo

    Robert Klemme wrote:

    > Tammo Tjarks wrote:
    >> Hello,
    >> I have seen a somehow strange behavior with Strings and the sub-method.
    >> Maybe you cen give me an idea if it is a bug and where it comes from.
    >> It have seen the problem when I wanted to make an substition with a
    >> sub(/<regexp>/,"") instead of (/<regexp/,'').
    >>
    >> To get the idea a example in irb. I tried it also with version 1.8.5 but
    >> have seen it first with 1.8.4:
    >>
    >> ====================================================
    >> irb(main):012:0> tst = "Das war ja nix"
    >> => "Das war ja nix"
    >> irb(main):013:0> tst2 =tst
    >> => "Das war ja nix"
    >> irb(main):014:0> tst2.sub(/nix/,"was")
    >> => "Das war ja was"
    >> irb(main):015:0> tst2.sub(/(n)+ix/,"$1")
    >> => "Das war ja $1"
    >> irb(main):016:0> tst2.sub(/(n)+ix/,"#{$1}")
    >> => "Das war ja n"
    >> irb(main):017:0> tst2.sub(/(n)+(i)+(x)+/,"#{$3}#{$2}#{$1}")
    >> => "Das war ja n"
    >> irb(main):018:0> tst2.sub(/(n)+(i)+(x)+/,'\3\2\1')
    >> => "Das war ja xin"
    >> irb(main):019:0> tst2.sub(/(n)+(i)+(x)+/,"#{$3}#{$2}#{$1}")
    >> => "Das war ja xin"
    >>
    >> ==================================================================
    >>
    >> What I do not understand is, that the substitution in the ''-expression
    >> with \1, \2 and \3 work without trouble but when I use the normal
    >> ""-String and the #{$1}, #{$2} and #{$3}-notation he seems alway to refer
    >> to the last regular expression and not the one in sub-method.

    >
    > That's easily explained: the string you hand off to sub is interpreted
    > before the call happens. But the grouping variables are only set after
    > that, i.e. when sub executes.
    >
    > The version with \1 etc. works, because method sub does the
    > interpretation of the string and replaces \1 with contents of the first
    > group etc:
    >
    > irb(main):005:0> puts( "\\1" )
    > \1
    > => nil
    > irb(main):006:0> puts( "#{$1}" )
    >
    > => nil
    >
    >> It is only an example. In the script I have seen it I cicrcumvent the
    >> problem by adding an extra regular expression before without using the
    >> result. But I think it is a strange behavior.

    >
    > Not at all once you understand how this works. It is the general
    > mechanism of method arguments being evaluated before the method call
    > starts. It's basically the same as:
    >
    > irb(main):001:0> def foo(x) puts x end
    > => nil
    > irb(main):002:0> foo( 1 + 2 )
    > 3
    > => nil
    >
    > Kind regards
    >
    > robert
     
    Tammo Tjarks, Nov 5, 2006
    #3
    1. Advertisements

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. THY
    Replies:
    1
    Views:
    537
    Steve C. Orr, MCSD
    Aug 19, 2003
  2. Kathy Burke
    Replies:
    3
    Views:
    604
    Kathy Burke
    Nov 24, 2003
  3. Tee
    Replies:
    2
    Views:
    1,035
  4. loga123
    Replies:
    2
    Views:
    435
    loga123
    May 20, 2006
  5. Frank Wallingford

    Sub-sub-expression evaluation order

    Frank Wallingford, Dec 9, 2004, in forum: C Programming
    Replies:
    4
    Views:
    465
    Chris Torek
    Dec 10, 2004
  6. Ben
    Replies:
    2
    Views:
    1,256
  7. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    3,109
    Lawrence D'Oliveiro
    May 20, 2011
  8. wpy

    how to create sub control within sub control?

    wpy, Apr 7, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    251
    Stefan
    Apr 8, 2004
Loading...