[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Peter

9/16/2003 10:12:00 PM

14 Answers

Hal E. Fulton

9/16/2003 10:29:00 PM

0

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>
>
> 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



mgarriss

9/16/2003 10:32:00 PM

0

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>
>
>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



Tim Bates

9/16/2003 11:18:00 PM

0

On Wed, Sep 17, 2003 at 07:29:04AM +0900, Hal Fulton wrote:
> 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
--
tim@bates.id.au

dagbrown

9/17/2003 2:05:00 AM

0

In article <Pine.GSO.4.10.10309162338320.27317-100000@iris.cs.kuleuven.ac.be>,
Peter <Peter.Vanbroekhoven@cs.kuleuven.ac.be> 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>
:
: 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

Jim Freeze

9/17/2003 2:30:00 AM

0

On Wednesday, 17 September 2003 at 7:12:16 +0900, Peter wrote:
> 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.

--
Jim Freeze
----------
Enzymes are things invented by biologists that explain things which
otherwise require harder thinking.
-- Jerome Lettvin

matz

9/17/2003 4:33:00 AM

0

Hi,

In message "#{} and \" don''t like each other"
on 03/09/17, Peter <Peter.Vanbroekhoven@cs.kuleuven.ac.be> writes:

|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.

Ben Giddings

9/17/2003 5:25:00 AM

0

Yukihiro Matsumoto wrote:
> 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


nobu.nokada

9/17/2003 6:46:00 AM

0

Hi,

At Wed, 17 Sep 2003 13:33:12 +0900,
Yukihiro Matsumoto wrote:
> |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?

--
Nobu Nakada

matz

9/17/2003 8:42:00 AM

0

Hi,

In message "Re: #{} and \" don''t like each other"
on 03/09/17, nobu.nokada@softhome.net <nobu.nokada@softhome.net> writes:

|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.

Robert Klemme

9/17/2003 9:12:00 AM

0


"Tim Bates" <tim@bates.id.au> schrieb im Newsbeitrag
news:20030916231638.GA31636@bates.id.au...
> On Wed, Sep 17, 2003 at 07:29:04AM +0900, Hal Fulton wrote:
> > 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?

=> Goedel

robert