# Operator precedence of assignment

**URL:** <https://www.ruby-forum.com/t/operator-precedence-of-assignment/80951>\
**Category:** Ruby\
**Created:** [January 2, 2007, 2:27pm UTC](https://www.ruby-forum.com/t/operator-precedence-of-assignment/80951 "2007-01-02T14:27:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [January 2, 2007, 2:27pm UTC](https://www.ruby-forum.com/t/operator-precedence-of-assignment/80951/1 "2007-01-02T14:27:36Z")

</div>

How do you think ruby parses this expression?  
a = 1 + b = 2 + c = 4 + d = 8

Originally I thought that operator precedence would result in  
a = (1 + b) = (2 + c) = (4 + d) = 8  
and since an assignment requires a variable, that would result in a  
SyntaxError.  
But instead it seems that everything on the right side of the assignment  
operator is evaluated first:  
a = (1 + (b = (2 + (c = (4 + (d = 8))))))

That had me really puzzled at first but I guess the behavior makes a  
certain  
sense, and it’s more useful than a syntax error. Ruby’s tolerant parser  
wins again!

Daniel

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [January 2, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/operator-precedence-of-assignment/80951/2 "2007-01-02T17:02:05Z")

</div>

Daniel DeLorme wrote:

> How do you think ruby parses this expression?  
> a = 1 + b = 2 + c = 4 + d = 8

Per…  
[http://phrogz.net/ProgrammingRuby/language.html#operatorexpressions](http://phrogz.net/ProgrammingRuby/language.html#operatorexpressions)  
…the + operator has much higher precedence than the assignment  
operator. That’s interesting that it doesn’t parse as you expected.  
It’s what I would have expected, too.

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [January 2, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/operator-precedence-of-assignment/80951/3 "2007-01-02T17:56:52Z")

</div>

Hi,

In message “Re: operator precedence of assignment”  
on Tue, 2 Jan 2007 22:26:53 +0900, Daniel DeLorme [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:  
|  
|How do you think ruby parses this expression?  
| a = 1 + b = 2 + c = 4 + d = 8  
|  
|Originally I thought that operator precedence would result in  
| a = (1 + b) = (2 + c) = (4 + d) = 8  
|and since an assignment requires a variable, that would result in a SyntaxError.  
|But instead it seems that everything on the right side of the assignment  
|operator is evaluated first:  
| a = (1 + (b = (2 + (c = (4 + (d = 8))))))  
|  
|That had me really puzzled at first but I guess the behavior makes a certain  
|sense, and it’s more useful than a syntax error. Ruby’s tolerant parser wins again!

Ah, for your information, that surprised me as well. Perhaps bison  
generated parser is smarter and eagerer than I expected. I have no  
idea what to do.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [January 2, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/operator-precedence-of-assignment/80951/4 "2007-01-02T18:06:56Z")

</div>

On 02.01.2007 17:56, Yukihiro M. wrote:

> |and since an assignment requires a variable, that would result in a SyntaxError.  
> |But instead it seems that everything on the right side of the assignment  
> |operator is evaluated first:  
> | a = (1 + (b = (2 + (c = (4 + (d = 8))))))  
> |  
> |That had me really puzzled at first but I guess the behavior makes a certain  
> |sense, and it’s more useful than a syntax error. Ruby’s tolerant parser wins again!
> 
> Ah, for your information, that surprised me as well. Perhaps bison  
> generated parser is smarter and eagerer than I expected. I have no  
> idea what to do.

I think Daniel did not want you to do anything - in fact he seems rather  
positively amazed. 🙂

(The only thing that comes to mind is thank him for the praise. :-))

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [January 2, 2007, 10:02pm UTC](https://www.ruby-forum.com/t/operator-precedence-of-assignment/80951/5 "2007-01-02T22:02:09Z")

</div>

On 02.01.2007 18:09, Yukihiro M. wrote:

> |(The only thing that comes to mind is thank him for the praise. :-))
> 
> The praise should go to yacc, not me. 😉

But _you_ chose it, didn’t you? So… 🙂

```
robert
```

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://www.ruby-forum.com/u/Daniel_DeLorme)\
**Post date:** [January 2, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/operator-precedence-of-assignment/80951/6 "2007-01-02T18:10:59Z")

</div>

Hi,

In message “Re: operator precedence of assignment”  
on Wed, 3 Jan 2007 02:05:06 +0900, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|I think Daniel did not want you to do anything - in fact he seems rather  
|positively amazed. 🙂

_That_ made me wonder what I should do.

|(The only thing that comes to mind is thank him for the praise. :-))

The praise should go to yacc, not me. 😉

```
						matz.

```
