Forum: Ruby Operator Precedence Conundrum

Dc7604e23c6f7ee8d1928929ba076e0e?d=identicon&s=25 Ja Sc (jgs)
on 2017-01-29 02:33
I don't get this:

2.4.0 :001 > (false && false ? false : true)
 => true

Interestingly:

2.4.0 :002 > (false and false ? false : true)
 => false

Parentheses can be used to produce the intended result:

2.4.0 :003 > (false && (false ? false : true))
 => false

But still the first case makes no sense to me.  Short circuit evaluation
would seem to dictate that the first instance should return false.

Thanks in advance for any enlightenment.
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-01-31 15:45
The && operator has higher precedense than ?:

The and operator has lower precedence than ?:
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.