From: Brian Candler Date: 2010-01-08T19:40:59+09:00 Subject: Re: Stopping String Escaping. Phil Cooper-king wrote: >> Hopefully it's clear from the above > yes, thanks you. > >> So try your test case again: >> (1) Use the DATA.read / __END__ to get the test source in >> (2) Use 'puts' and not 'dump' to see clearly what you have > > [code] ... > data_read = DATA.read > string = "\\" ... > __END__ > \\ > [/code] So in this program, 'data_read' contains two backslash characters; and 'string' contains a single backslash character. > yeilds >

\

>

\\

That looks correct to me - HTML doesn't need a backslash to be escaped. So now add Uv into your test to see if that is munging the backslashes. > although I have no idea how to treat a string as a file. A string is just a string. In ruby 1.8 it's a sequence of bytes; in ruby 1.9 it's a sequence of characters. But that doesn't matter here; a backslash is a backslash, and is both a single character and a single byte in either ASCII or UTF-8. However if you enter a string *literal* in a ruby program (or in IRB), then it is parsed with backslash escaping rules to turn it into an actual String object. For example: a = "abc\ndef" b = 'abc\ndef' string 'a' contains 7 characters (a,b,c,newline,d,e,f), whereas string b contains 8 characters (a,b,c,backslash,n,d,e,f). This is because there are different escaping rules for double-quoted and single-quoted strings. In a single-quoted string literal, \' is a single quote, and \\ is a backslash, and everything else is treated literally, so \n is two characters \ and n. In a double-quoted string literal, \" is a double quote, \n is a newline, \\ is a backslash, and there's a whole load of other expansion including #{...} for expression interpolation and #@... for instance variable substitution. > erb results are similar, which I would have though was be happening in > rails anyway >>> ERB.new("\\").src > => "_erbout = ''; _erbout.concat \"\\\\\"; _erbout" Now you're just scaring yourself with backslash escaping :-) Firstly, note that you passed a single backslash character to ERB. That's what the string literal "\\" creates. ERB compiled it to the following Ruby code: _erbout = ''; _erbout.concat "\\"; _erbout which just appends a single backslash to _erbout, which is what you expect. However, IRB displays the returned string from ERB.new using String#inspect, so it is turned into a double-quoted string. This means: 1. A " is added to the start and end of the string 2. Any " within the string is displayed as \" 3. Any \ within the string is displayed as \\ In other words, String#inspect turns a string into a Ruby string literal - something that you could paste directly into IRB. Try it: >> str = "_erbout = ''; _erbout.concat \"\\\\\"; _erbout" >> puts str That will show you the actual contents of str, which is the Ruby code I pasted above. HTH, Brian. -- Posted via http://www.ruby-forum.com/.