From: Robert Klemme Date: 2007-02-22T22:17:26+09:00 Subject: Re: Weird behaviour escaping special characters in a string 2007/2/21, Greg Hurrell : > 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 > > The idea is that it will give you a string that you can write out a > Ruby file that will later print the string. For, example, let's take > the string, foo (3 characters): > > "puts '" + "foo".to_source_string + "'" # puts 'foo' > > Or a string with special characters in it like 'foo' (5 characters, > including enclosing single quotes): > > "puts '" + "'foo'".to_source_string + "'" # puts '\'foo\'' Why don't you just use #inspect? Kind regards robert