Todd Benson
4/12/2008 4:04:00 PM
On Sat, Apr 12, 2008 at 9:25 AM, Christopher Dicely <cmdicely@gmail.com> wrote:
> On Fri, Apr 11, 2008 at 9:20 AM, matt neuburg <matt@tidbits.com> wrote:
> > irb(main):001:0> s = "\\\\"
> > => "\\\\"
> > irb(main):002:0> s.length
> > => 2
> > irb(main):003:0> s = "howdy".sub("howdy", s)
> > => "\\"
> > irb(main):004:0> s.length
> > => 1
> >
>
> > So merely using a string as the second param of sub (the replacement
> > value) can cause that string to be altered.
>
> Nope, using the string (s) as the second parameter of sub did nothing to alter
> it. This is clear if you use a different variable as the assignment target:
>
>
>
> irb(main):001:0> s='\\\\'
> => "\\\\"
> irb(main):002:0> s.length
> => 2
> irb(main):003:0> foo = "howdy".sub("howdy",s)
>
> => "\\"
> irb(main):004:0> s
> => "\\\\"
> irb(main):005:0> s.length
> => 2
> irb(main):006:0> foo
> => "\\"
> irb(main):007:0> foo.length
> => 1
>
> s isn't changed by being used as the second argument to sub, instead, the
> string sent as the second argument to sub is processed for escape sequences
> so that the substring '\\' occurring in that string is treated as a
> single literal '\'
> when used in the replacement.
>
> But its not changed, as the above irb session shows. s is unmodified.
When I first read the post, I immediately wanted to strike out with a
"well, you're assigning" response.
I'm not sure, but I think the OP was referring to what you said;
namely, how the escaping happens before subbing.
Todd