On Tuesday, September 16, 2003, at 06:12 PM, Peter wrote:

> From the Programming Ruby book:
> <quote>
>   In addition, you can substitute the value of any Ruby expression
>   into a string using the sequence #{ expr }.
> </quote>
>
> [snip]

>   name = "peter"
>   temp = "name=\"" + name + "\""
>   print "<user #{temp}></user>"

> [snip]

> One would expect that substituting the RHS of the assignment to temp 
> for
> temp in the third statement, would work as well:

> [snip]

>   (irb):2: warning: escaped terminator '"' inside string interpolation
>   SyntaxError: compile error
>   (irb):3: unterminated string meets end of file
>   (irb):3: syntax error
>           from (irb):3
>
> Anyway, what happens is that the escaped quotes are misinterpreted 
> since
> the problem is gone when I leave them out.

> [snip]

As you may have already discovered, the following gives the expected 
results:

print "<user #{"name=" + name + ""}></user>"

In the variable assignment, you need to escape the quotes because 
otherwise the literal string you are creating would be considered 
terminated. Inside a literal string, inside #{}, it's not necessary, 
because we indicate the end of the expression to be interpolated with 
an unescaped '}'. In addition, if we interpreted '\"' inside #{}, we 
would not be able to terminate the parent string literal after the 
appearance of '#{', which would be bad (because there is a sequence of 
characters we can't put in a string literal, ever).

Regards,

Mark