#{} and \" don't like each other

P

Peter

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>

Up to about an hour ago, I never doubted this statement. I've stumbled
upon the following example which is not accepted by ruby. The following
piece of code works perfectly, as to be expected:

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

This prints exactly what I want it to print, namely:

<user name="peter" ></user>

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

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

Type this in at the ruby prompt and you'll soon notice that it gives you
the following prompt and not the expected output as above:

irb(main):003:-1/

Note the -1 in the prompt, meaning ruby managed to get above the toplevel.

Ruby just keeps waiting for input, and I can type anything but ruby will
stay in this state until I type a / and then unhappily states this:

(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. The effect is that the slash in
</user> is interpreted as the start of a regular expression. This can only
be if ruby thinks it's not part of a string string, which was clearly not
my intention.

Can someone please tell me whether this is a bug, or otherwise why I
shouldn't have done the above.

Note: I'm using version 1.8.0 of ruby, don't know if the trueness of the
above statement from Programming Ruby changed as the ruby version changed.

Thanks,
Peter
 
H

Hal Fulton

Peter said:
<quote>
In addition, you can substitute the value of any Ruby expression
into a string using the sequence #{ expr }.
</quote>

Up to about an hour ago, I never doubted this statement. I've stumbled
upon the following example which is not accepted by ruby.

Fulton's Third Law says that there is an exception to every rule,
except Fulton's Third Law.
Can someone please tell me whether this is a bug, or otherwise why I
shouldn't have done the above.

Truthfully, I'm not sure why this happens. But since the
warning is accurate and explicit, I don't think it's a bug,
but perhaps one of those features that's it's not immediately
obvious we need.

FWIW, this avoids it:

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

So it evidently has to do specifically with the escaped terminator
*inside* the interpolated part.

Hal
 
M

Michael Garriss

Peter said:
<quote>
In addition, you can substitute the value of any Ruby expression
into a string using the sequence #{ expr }.
</quote>

Up to about an hour ago, I never doubted this statement. I've stumbled
upon the following example which is not accepted by ruby. The following
piece of code works perfectly, as to be expected:

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

This prints exactly what I want it to print, namely:

<user name="peter" ></user>

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

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

Type this in at the ruby prompt and you'll soon notice that it gives you
the following prompt and not the expected output as above:

irb(main):003:-1/

Note the -1 in the prompt, meaning ruby managed to get above the toplevel.

Ruby just keeps waiting for input, and I can type anything but ruby will
stay in this state until I type a / and then unhappily states this:

(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. The effect is that the slash in
</user> is interpreted as the start of a regular expression. This can only
be if ruby thinks it's not part of a string string, which was clearly not
my intention.

Can someone please tell me whether this is a bug, or otherwise why I
shouldn't have done the above.

Note: I'm using version 1.8.0 of ruby, don't know if the trueness of the
above statement from Programming Ruby changed as the ruby version changed.
print "<user #{'name=\"' + name + '\"'}></user>"

This works, don't know about why the other one doesn't though.....

Michael
 
T

Tim Bates

Fulton's Third Law says that there is an exception to every rule,
except Fulton's Third Law.

So what your saying is, Fulton's Third Law has no exception? Does that
not make the Law its own exception? And if it has an exception, does
that not make the second clause paradoxical, or at least wrong?

Or was that the point?

Tim Bates
 
D

Dave Brown

: <quote>
: In addition, you can substitute the value of any Ruby expression
: into a string using the sequence #{ expr }.
: </quote>
:
: Up to about an hour ago, I never doubted this statement. I've stumbled
: upon the following example which is not accepted by ruby. The following
: piece of code works perfectly, as to be expected:
:
: name = "peter"
: temp = "name=\"" + name + "\""
: print "<user #{temp}></user>"
:
: This prints exactly what I want it to print, namely:
:
: <user name="peter" ></user>
:
: One would expect that substituting the RHS of the assignment to temp for
: temp in the third statement, would work as well:
:
: name = "peter"
: print "<user #{"name=\"" + name + "\""}></user>"

Okay, I think the problem is this:

print "<user #{ # <- start Ruby interpolation in string
"name= # <- start string in interpolation
\" # <- What's the backslash escaping?

It looks like it should be escaping for the benefit of the string
inside the interpolation inside the string, but who knows? Maybe
the outside string is seeing it and that's confusing the
interpreter. I think this definitely needs Matz to take a look at
it and figure out what should be going on here (and issue a royal
proclamation about the correct behaviour).

Personally, I think "string inside interpolation inside string" is
a pretty dubious construct to be trying to use in the first place,
and redundant anyway, when you could simply be doing:

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

(Or, if this is supposed to be XML, "<user name=\"#{name}\"/>" ;-) )

--Dave
 
J

Jim Freeze

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

When I see a line like this, my first question is why not
write it like:

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

It looks cleaner and avoids all the hassle and confusion.
 
Y

Yukihiro Matsumoto

Hi,

In message "#{} and \" don't like each other"

|One would expect that substituting the RHS of the assignment to temp for
|temp in the third statement, would work as well:
|
| name = "peter"
| print "<user #{"name=\"" + name + "\""}></user>"

I consider it as a bug. Avoid escaped double quotes in the string
interpolation until it's fixed, e.g.

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

Only Nobu knows this part of the parser.

matz.
 
B

Ben Giddings

Yukihiro said:
I consider it as a bug. Avoid escaped double quotes in the string
interpolation until it's fixed, e.g.

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

Only Nobu knows this part of the parser.

Heh -- a polite way of saying it's Nobu's bug. *grin*

But seriously, it sure is refreshing to know that matz will see an issue
within a day at the most, and will immediately know and say whether or
not it truly is a bug. Thanks matz (and Nobu too).

Ben
 
N

nobu.nokada

Hi,

At Wed, 17 Sep 2003 13:33:12 +0900,
Yukihiro said:
|One would expect that substituting the RHS of the assignment to temp for
|temp in the third statement, would work as well:
|
| name = "peter"
| print "<user #{"name=\"" + name + "\""}></user>"

I consider it as a bug. Avoid escaped double quotes in the string
interpolation until it's fixed, e.g.

I've thought, according to the thread from [ruby-dev:17421],
and [ruby-dev:17549], it's intentional (but transitional).
It's not tough to change it, will we do it now?
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: #{} and \" don't like each other"

|I've thought, according to the thread from [ruby-dev:17421],
|and [ruby-dev:17549], it's intentional (but transitional).
|It's not tough to change it, will we do it now?

Please. 1.8.1 should work as it is.

matz.
 
R

Robert Klemme

Tim Bates said:
So what your saying is, Fulton's Third Law has no exception? Does that
not make the Law its own exception? And if it has an exception, does
that not make the second clause paradoxical, or at least wrong?

Or was that the point?

=> Goedel

robert
 
N

nobu.nokada

Hi,

At Wed, 17 Sep 2003 17:42:17 +0900,
Yukihiro said:
|I've thought, according to the thread from [ruby-dev:17421],
|and [ruby-dev:17549], it's intentional (but transitional).
|It's not tough to change it, will we do it now?

Please. 1.8.1 should work as it is.

Is it OK without warnings? That is, "#{"\"\""}" is evaluated
as "" till now, but it will become "\"\"" from now.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: #{} and \" don't like each other"

|> Please. 1.8.1 should work as it is.
|
|Is it OK without warnings? That is, "#{"\"\""}" is evaluated
|as "" till now, but it will become "\"\"" from now.

I suppose all such programs have warned during 1.8.0 period.

matz.
 
M

Mike Stok

When I see a line like this, my first question is why not
write it like:

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

It looks cleaner and avoids all the hassle and confusion.

What about using %Q to pick your own delimieters e.g.

print %Q{<user name="#{name}" />}

I try to avoid \ wherever possible.

Hope this helps,

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top