While debugging some ruby grok bugs, I found something very strange where String#sub replaces all double backslashes with single backslashes
Input: hello world
Expected result: Hurray for slashes! \\testing//
Actual result: Hurray for slashes! \testing//
Equal: false
This is quite confusing. I said "replace this string with that string" and it took the 'that string' and replaced all double backslashes with single backslashes? This is very strange and certainly a bug.
This is probably an integration problem due to String#sub supporting captured groups when used with regexps; from the ruby docs - http://www.ruby-doc.org/core-1.9.3/String.html :
If replacement is a String it will be substituted for the matched text. It may contain back-references to the pattern’s capture groups of the form \d, where d is a group number, or \k, where n is a group name. If it is a double-quoted string, both back-references must be preceded by an additional backslash. However, within replacement the special match variables, such as &$, will not refer to the current match.
I think this is a bug. If the first argument to String#sub() is a string, then there will be no capturing so there is no need to process backslashes and capture groups in the replacement string.
@headius - I buy the \0 usage, but not fully. If you do:
It would be just as trivial to, say, do this:
And probably wouldn't be as confusing to me. Still, I suppose I am glad that all rubies do this and not just one ruby ;)