WoNáDo
1/21/2007 4:46:00 PM
Wolfgang Nádasi-Donner schrieb:
> Ooops
>
> irb(main):005:0> puts "a string with \\ a backslash".gsub(/\\/, "\\\\\\\\")
> a string with \\ a backslash
>
> This works as expected, but why it works with '\'*6 I don't understand.
Even after looking to "Programming Ruby - Second Edition", and several other
places, I have no idea why it works with '\\\\\\', and other strange things like
this:
irb(main):001:0> x = "a\\b"
=> "a\\b"
irb(main):002:0> puts x
a\b
=> nil
irb(main):003:0> puts x.gsub(/\\/, '+\\\\\\+')
a+\b
=> nil
irb(main):004:0> puts x.gsub(/\\/, '+\\\\\\\\+')
a+\\+b
=> nil
irb(main):005:0> puts x.gsub(/\\/, '+\\\\\\t')
a+\\tb
=> nil
irb(main):006:0> puts x.gsub(/\\/, '+\\\\\\\\t')
a+\\tb
=> nil
Especially if I take the following into account:
irb(main):001:0> x = '\\\\'
=> "\\\\"
irb(main):002:0> puts x
\=> nil
irb(main):003:0> puts Regexp.escape(x)
\\\=> nil
irb(main):004:0> Regexp.escape(x) == '\\\\\\\\'
=> true
irb(main):005:0> Regexp.escape(x) == '\\\\\\'
=> false
Wolfgang Nádasi-Donner