I assume you are talking about the following problem:
"\\n".gsub(/\\\\/, "\\").gsub(/\\n/, "\n") # => "n" "\\n".gsub(/\\n/, "\n").gsub(/\\\\/, "\\") # => "\\\n"
String#gsub can take an argument from a block that does the substitution.
str.gsub(/\\(.)/) do |s| case $1 when "n" "\n" when "t" "\t" else $1 end end
Thus, no special escape sequence is replaced by the first, and everything works as expeted.
johannes Oct 31 '09 at 12:13 2009-10-31 12:13
source share