Dear List,

took me hours to find out, where my backslashes magically disappeared.
finally found something like this:

r = "value: <% replace_me %>"
s = "\"\\\" some quotations \\\" \\\\\\\"really weird quotation\\\\\\\"\""

# try a regexp
reg = /(<%\s*replace_me\s*%>)/
p r.gsub(reg, s)
# => "value: \"\\\" some quotations \\\" \\\\\"really weird
quotation\\\\\"\""

while its documented and arguable that backslashes are interpreted they
should not disappear IMHO

no_reg = "<% replace_me %>"
p r.gsub(no_reg, s)
# => "value: \"\\\" some quotations \\\" \\\\\"really weird
quotation\\\\\"\""

but if we use no regexp it really makes no sense to interpret backslashes
here. who needs/expects this?

p r.gsub(no_reg, '\1\2')
# => "value: "


If you use lots of regexps in a recursive loop down a chain of in string
substitutions such things are really hard to debug, believe me.

Now the solution is to use a block instead of the second parameter.

p r.gsub(no_reg){ s }
# => "value: \"\\\" some quotations \\\" \\\\\\\"really weird
quotation\\\\\\\"\""

In fact I do need the bahaviour with the block a lot more often than \1 &
friends. So I am going to replace it everywhere in my code. 

Just wanted to share the experience, I guess there might be some people out
there who weren't aware of that behaviour.

It wasn't the principle of my least surprise this time :)


have a good night,


benny