From: James Edward Gray II Date: 2007-02-22T03:45:07+09:00 Subject: Re: Weird behaviour escaping special characters in a string On Feb 21, 2007, at 12:36 PM, Austin Ziegler wrote: > On 2/21/07, Greg Hurrell wrote: >> This instance method added to the String class returns a copy of the >> receiver with occurrences of \ replaced with \\, and occurrences of ' >> replaced with \': >> >> class String >> def to_source_string >> gsub(/(\\|')/, '\\\\\1') >> end >> end > > class String > def to_source_string > gsub(/(\\|')/) { "\\#$1" } > end > end It's probably better to use a character class [\\'] instead of alternation (\\|'). James Edward Gray II