8

Some information source on operator precedence like this says that unary operators like !, ~, +, - have higher precedence than assignment =. However, the following expressions are possible:

!a = true # => false (with warning) a # => true ~a = 1 # => -2 a # => 1 +a = 1 # => 1 a # => 1 -a = 1 # => -1 a # => 1 

Considering these results, the only possible explanation I can think of is that these unary operator have lower precedence than the assignment. If that is the case, then it would mean that the information I mentioned above is wrong. Which is correct? Is there a different explanation?

3
  • 2
    Interesting, but this behavior makes perfect sense, as applying a unary operator to an lvalue before the assignment would have no effect. Docs should address this, of course. Commented Feb 23, 2014 at 20:03
  • @CarySwoveland You are right.. Commented Feb 23, 2014 at 20:13
  • 2
    Just to be clear, ~a = 1 appears to behave like ~(a = 1). Commented Feb 23, 2014 at 20:25

1 Answer 1

2

My programming ruby book (2nd edition) also lists unary operators as having higher precedence than assignment.

The unary operator IS being given highest precedence. The reason the line is parsed as ~ (a = 1) is because decomposing the line into valid syntax is of higher precedence than anything else, including using the simple variable 'a' as the expression the unary operator operates on.

If the ruby parser could have made something valid of the rest of the line, it would have used (~ a), but there is no valid rule than matches = something, only lvalue '=' rvalue.

You can regard "valid syntax" as the top priority, then simple values, constant and variable names and then the standard operators under that.

Sign up to request clarification or add additional context in comments.

4 Comments

I think you are right. Actually, I was also thinking along the same line. But then, when would the statement become non-trivial that unary operators have higher precedence than assignment? Is there an example where two precedence interpretations are valid, and this rule comes into play?
I can't think of a situation where there would be ambiguity between assignment and any unary operator because a. unary operators produce expressions, b. unary operators operate on single expressions to their right, c. expressions can't be used as the left of assignments and d. the only expression used in an assignment is a single expression on the right. Binary operators are different, for example a = 3 and b = 4 produce different results to a = 3 && b = 4 (because and is lower and && is higher precedence than assignment).
I am curious if someone can invent a counter example - my gut says it may be a case of it is easier to explain why I think I am right than realise I'm wrong ;)
I found a useful ruby parser gem: github.com/whitequark/parser - it will output how the expression is parsed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.