precedence of single-line rescue and assignment

M

matt neuburg

def boom
raise "boom"
end
y = ""
y = boom rescue "fine"
puts y # "fine"

So far so good; the precedence of "rescue" is higher than that of
assignment. But now:

def boom
raise "boom"
end
y = ""
y += boom rescue "fine"
puts y # ""

How does the precedence work here, and should this behavior be
considered a possible bug? m.
 
M

matt neuburg

matt neuburg said:
def boom
raise "boom"
end
y = ""
y += boom rescue "fine"
puts y # ""

I think what's really going on here is that what we often see described
as a single-line use of "rescue" is actually a single-*expression* use
of "rescue", since the above can be fixed using parentheses:

y += (boom rescue "fine")

I hadn't realized this was even legal. m.
 
A

Albert Schlef

matt said:
[...] since the above can be fixed using parentheses:

y += (boom rescue "fine")

But if `y += boom rescue "fine"` indeed translates into `y += (boom
rescue "fine")` that still doesn't solve the mystery. We'd end up with
`y += ("fine")`. We'd get `y == "fine"', which isn't the case...

That's strange!
 
J

Jens Wille

Albert Schlef [2008-05-07 21:27]:
matt said:
[...] since the above can be fixed using parentheses:

y += (boom rescue "fine")

But if `y += boom rescue "fine"` indeed translates into `y +=
(boom rescue "fine")` that still doesn't solve the mystery.
no, it evaluates to '(y += boom) rescue "fine"'. the '+=' binds
tighter than/takes precedence over 'rescue'.

def boom; p 'boom'; raise 'boom'; end
=>nil
def fine; p 'fine'; 'fine'; end
=>nil
y = ''
=>""
y += boom rescue fine
"boom"
"fine"
=>"fine"
y
=>""
(y += boom) rescue fine
"boom"
"fine"
=>"fine"
y
=>""
y += (boom rescue fine)
"boom"
"fine"
=>"fine"
y
=>"fine"

cheers
jens

--
Jens Wille, Dipl.-Bibl. (FH)
prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre
Kunsthistorisches Institut der Universität zu Köln
Albertus-Magnus-Platz, D-50923 Köln
Tel.: +49 (0)221 470-6668, E-Mail: (e-mail address removed)
http://www.prometheus-bildarchiv.de/
 
M

matt neuburg

Jens Wille said:
Albert Schlef [2008-05-07 21:27]:
matt said:
[...] since the above can be fixed using parentheses:

y += (boom rescue "fine")

But if `y += boom rescue "fine"` indeed translates into `y +=
(boom rescue "fine")` that still doesn't solve the mystery.
no, it evaluates to '(y += boom) rescue "fine"'. the '+=' binds
tighter than/takes precedence over 'rescue'.

Right, that's what I said. The mystery is why

y += boom rescue "fine"

translates to

(y += boom) rescue "fine"

but

y = boom rescue "fine"

translates to

y = (boom rescue "fine")

What I'm saying is: Doesn't the user reasonably expect that = and +=
will have similar precedence? I don't really care about this any more,
now that I know that I can disambiguate for myself using parentheses;
but it feels like a bug, somehow. m.
 
J

Jens Wille

matt neuburg [2008-05-07 22:15]:
What I'm saying is: Doesn't the user reasonably expect that = and
+= will have similar precedence?
well, maybe yes. the pickaxe even lists them all as having the same
precedence [1]. but as you can easily verify, all operator
assignments in fact have a higher precedence than plain assignment.

maybe it's because "lhs '=' arg kRESCUE_MOD arg" is a special case
in parse.y [2] over the more generic "stmt kRESCUE_MOD stmt"? i
don't know. i'm far from claiming that i have grokked ruby's parsing ;-)

however, whether this is to be considered a bug, i'm not in the
position to judge...

[1]
<http://phrogz.net/ProgrammingRuby/language.html#operatorexpressions>
[2] <http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8/parse.y>

cheers
jens
 
R

Robert Dober

Jens Wille said:
Albert Schlef [2008-05-07 21:27]:
matt neuburg wrote:
[...] since the above can be fixed using parentheses:

y += (boom rescue "fine")

But if `y += boom rescue "fine"` indeed translates into `y +=
(boom rescue "fine")` that still doesn't solve the mystery.
no, it evaluates to '(y += boom) rescue "fine"'. the '+=' binds
tighter than/takes precedence over 'rescue'.

Right, that's what I said. The mystery is why

y += boom rescue "fine"

translates to

(y += boom) rescue "fine"

but

y = boom rescue "fine"

translates to

y = (boom rescue "fine")

What I'm saying is: Doesn't the user reasonably expect that = and +=
will have similar precedence? I don't really care about this any more,
now that I know that I can disambiguate for myself using parentheses;
but it feels like a bug, somehow. m.

And worse

y += boom rescue fine

is not equivalent to

y = y + boom rescue fine

that is not what I understand under syntactic sugar, but the behavior
is all the same in MRI1.8 YARV and Jruby, that is not a nice thing.
Well spotted indeed.

Cheers
Robert
 
M

matt neuburg

Yukihiro Matsumoto said:
Hi,

In message "Re: precedence of single-line rescue and assignment"
on Thu, 8 May 2008 10:10:07 +0900, (e-mail address removed) (matt neuburg) writes:

|> y += boom rescue fine
|>
|> is not equivalent to
|>
|> y = y + boom rescue fine
|>
|> that is not what I understand under syntactic sugar, but the behavior
|> is all the same in MRI1.8 YARV and Jruby, that is not a nice thing.
|> Well spotted indeed.
|
|If we think this might be a bug, how do I report it? m.

I recognized the problem.

Wonderful, thanks! m.
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top