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.
I don't think this a bug as much as a peculiar edge case. Keep in mind that even with a String there's still a group available: https://gist.github.com/1491509
I think it would be odd to have backrefs only be processed when passing in a Regexp, since there's almost certainly cases out there using the \0 pattern with a String.