From: Christopher Dicely Date: 2008-04-12T23:25:43+09:00 Subject: Re: surprise in sub On Fri, Apr 11, 2008 at 9:20 AM, matt neuburg 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.