[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

precedence of single-line rescue and assignment

matt

5/7/2008 5:37:00 PM

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.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...
7 Answers

matt

5/7/2008 6:53:00 PM

0

matt neuburg <matt@tidbits.com> wrote:

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

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Albert Schlef

5/7/2008 7:27:00 PM

0

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. We'd end up with
`y += ("fine")`. We'd get `y == "fine"', which isn't the case...

That's strange!

--
Posted via http://www.ruby-....

Jens Wille

5/7/2008 7:51:00 PM

0

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

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: jens.wille@uni-koeln.de
http://www.prometheus-bild...

matt

5/7/2008 8:11:00 PM

0

Jens Wille <jens.wille@uni-koeln.de> wrote:

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

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Jens Wille

5/7/2008 9:34:00 PM

0

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#operatorexpre...
[2] <http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8/p...

cheers
jens

Robert Dober

5/7/2008 11:55:00 PM

0

On Wed, May 7, 2008 at 10:15 PM, matt neuburg <matt@tidbits.com> wrote:
> Jens Wille <jens.wille@uni-koeln.de> wrote:
>
> > 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
>
>
>
> --
> matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
> Leopard - http://www.takecontrolbooks.com/leopard-custom...
> AppleScript - http://www.amazon.com/gp/product/...
> Read TidBITS! It's free and smart. http://www.t...
>
>



--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

matt

5/8/2008 1:04:00 AM

0

Robert Dober <robert.dober@gmail.com> wrote:

> On Wed, May 7, 2008 at 10:15 PM, matt neuburg <matt@tidbits.com> wrote:

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

If we think this might be a bug, how do I report it? m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...