Escaping is limited when using single quotes rather than double quotes:
puts 'sinlge\nquote' puts "double\nquote"
"\0" is the null character (used in C to determine the end of the line), where '\0' is "\\0" , so both are 'hello'.gsub(/.+/, '\0') and 'hello'.gsub(/.+/, "\\0") returns "hello" , but 'hello'.gsub(/.+/, "\0") returns "\000" . Now 'hello'.gsub(/.+/, '\\0') returning 'hello' is a ruby โโtrying to cope with programmers without preserving the difference between single and double quotes. In fact, this has nothing to do with gsub : '\0' == "\\0" and '\\0' == "\\0" . Following this logic, no matter how you think about it, this is how Ruby sees other lines: both '\\\0' and '\\\\0' are equal to "\\\\0" , which (when printed) give you \\0 . Since gsub uses \x to insert the number of matches x, you need the escape \x method, which is \\x , or in its lowercase representation: "\\\\x" .
Therefore line
puts 'hello'.gsub(/.+/, "\\0 \\\\0 \\\\\\0 \\\\\\\\0")
really leads to
hello \0 \hello \\0
Konstantin Haase Jun 12 '10 at 13:21 2010-06-12 13:21
source share