grammar - Understanding precedence of assignment and logical operator in Ruby -


i can't understand precedence of ruby operators in following example:

x = 1 && y = 2 

since && has higher precedence =, understanding + , * operators:

1 + 2 * 3 + 4 

which resolved as

1 + (2 * 3) + 4 

it should equal to:

x = (1 && y) = 2 

however, ruby sources (including internal syntax parser ripper) parse as

x = (1 && (y = 2)) 

why?


edit [08.01.2016]

let's focus on subexpression: 1 && y = 2

according precedence rules, should try parse as:

(1 && y) = 2 

which doesn't make sense because = requires specific lhs (variable, constant, [] array item etc). since (1 && y) correct expression, how should parser deal it?

i've tried consulting ruby's parse.y, it's spaghetti-like can't understand specific rules assignment.

simple. ruby interprets in way makes sense. = assignment. in expectation:

x = (1 && y) = 2 

it not make sense assign 1 && y. can assign variable or constant.

and note purpose of precedence rule disambiguate otherwise ambiguous expression. if 1 way interpret not make sense, there no need disambiguation, , hence precedence rule not in effect.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -