[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

operator precedence of assignment

Daniel DeLorme

1/2/2007 1:27:00 PM

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

1 Answer

Gavin Kistner

1/2/2007 3:56:00 PM

0

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