operator precedence of assignment

D

Daniel DeLorme

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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top