# Precedence of single-line rescue and assignment

**URL:** <https://www.ruby-forum.com/t/precedence-of-single-line-rescue-and-assignment/136967>\
**Category:** Ruby\
**Created:** [May 7, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/precedence-of-single-line-rescue-and-assignment/136967 "2008-05-07T19:40:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [May 7, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/precedence-of-single-line-rescue-and-assignment/136967/1 "2008-05-07T19:40:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [May 7, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/precedence-of-single-line-rescue-and-assignment/136967/2 "2008-05-07T20:56:18Z")

</div>

matt neuburg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [May 7, 2008, 9:27pm UTC](https://www.ruby-forum.com/t/precedence-of-single-line-rescue-and-assignment/136967/3 "2008-05-07T21:27:23Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [May 7, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/precedence-of-single-line-rescue-and-assignment/136967/4 "2008-05-07T22:16:03Z")

</div>

Jens W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Albert S. [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.

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [May 7, 2008, 11:34pm UTC](https://www.ruby-forum.com/t/precedence-of-single-line-rescue-and-assignment/136967/5 "2008-05-07T23:34:21Z")

</div>

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](http://phrogz.net/ProgrammingRuby/language.html#operatorexpressions)  
[2] [http://svn.ruby-lang.org/repos/ruby/branches/ruby\_1\_8/parse.y](http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8/parse.y)

cheers  
jens

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [May 7, 2008, 9:52pm UTC](https://www.ruby-forum.com/t/precedence-of-single-line-rescue-and-assignment/136967/6 "2008-05-07T21:52:28Z")

</div>

Albert S. [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

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [May 8, 2008, 1:55am UTC](https://www.ruby-forum.com/t/precedence-of-single-line-rescue-and-assignment/136967/7 "2008-05-08T01:55:34Z")

</div>

On Wed, May 7, 2008 at 10:15 PM, matt neuburg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > no, it evaluates to ‘(y += boom) rescue “fine”’. the ‘+=’ binds  
> > but  
> > 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 = [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid), [Matt Neuburg’s Home Page](http://www.tidbits.com/matt/)  
> Leopard - [http://www.takecontrolbooks.com/leopard-customizing.html](http://www.takecontrolbooks.com/leopard-customizing.html)  
> AppleScript - [AppleScript: The Definitive Guide, 2nd Edition: Neuburg, Matt: 9780596102111: Amazon.com: Books](http://www.amazon.com/gp/product/0596102119)  
> Read TidBITS! It’s free and smart. [http://www.tidbits.com](http://www.tidbits.com)

–

> **[Smalltalking about Ruby](http://ruby-smalltalk.blogspot.com/)**

* * *

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

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [May 8, 2008, 4:08am UTC](https://www.ruby-forum.com/t/precedence-of-single-line-rescue-and-assignment/136967/8 "2008-05-08T04:08:21Z")

</div>

Hi,

In message “Re: precedence of single-line rescue and assignment”  
on Thu, 8 May 2008 10:10:07 +0900, removed\_email\_address@domain.invalid (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.

```
          matz.
```

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [May 8, 2008, 3:11am UTC](https://www.ruby-forum.com/t/precedence-of-single-line-rescue-and-assignment/136967/9 "2008-05-08T03:11:02Z")

</div>

Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Wed, May 7, 2008 at 10:15 PM, matt neuburg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [May 8, 2008, 6:40am UTC](https://www.ruby-forum.com/t/precedence-of-single-line-rescue-and-assignment/136967/10 "2008-05-08T06:40:59Z")

</div>

Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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