
On Feb 19, 2024, at 02:22, botp via ruby-talk <ruby-talk@ml.ruby-lang.org> wrote:
1 and 2 or 3 and 4 => 4
evaluation left to right
1 && 2 || 3 && 4 => 2
evaluation 1st: 1 && 2 => 2 evaluation 2nd: 3 && 4 => 4 evaluation last: 2 || 4 => 2
not quite. the LHS isn't evaluated: #>>> 1 && 2 || raise("NO!") && 4 # => 2
"or" and "and" have same priority. "&&" precedes "||".
put another way, slightly more visual:
% rake debug R="1 && 2 || 3 && 4" s(:or, s(:and, s(:lit, 1), s(:lit, 2)), s(:and, s(:lit, 3), s(:lit, 4)))
which is what everyone expects, versus:
% rake debug R="1 and 2 or 3 and 4" s(:and, s(:or, s(:and, s(:lit, 1), s(:lit, 2)), s(:lit, 3)), s(:lit, 4))
which is the same as:
% rake debug R="((1 and 2) or 3) and 4" s(:and, s(:or, s(:and, s(:lit, 1), s(:lit, 2)), s(:lit, 3)), s(:lit, 4))