Try the following code:
s = '#value#'
puts s.gsub('#value#', Regexp.escape('*')) # => '\*'
puts s.gsub('#value#', Regexp.escape('+')) # => ''
Wtf? It looks like the char '\+' (returned by Regexp.escape) is completely ignored by gsub. How to fix this?
xsdg of #ruby worked this out
Looks like that gsub’s replacement is parsed, so the + is lost somewhere in the process. A workaround is using gsub’s block syntax. This way:
Works as expected 🙂
Thanks, xsdg!