Shared publicly  - 
 
I learned another very important thing about Ruby yesterday, which honestly I should have already known by heart. In Ruby there are operators such as 'and' and 'or', which do essentially what you would expect. There are also '&&' and '||' operators. There is also a 'not' operator the behaves similarly to the '!' operator.

Despite having been writing some Ruby for awhile now, I somehow didn't notice until yesterday that these operators are not just aliases for one another. I had been assuming that 'and' is just a syntactic alias for &&, but in fact it has a much lower precedence order than && does. The same is true of 'or' vs. ||, and 'not' vs. !

While && and || have higher precedence than the ternary ?: operator, 'and' and 'or' do not. The '!' operator is one of the highest in the order of precedence, while 'not' is one of the lowest.
1
Cody Russell's profile photoKalle Valo's profile photoMike Vincent's profile photoLiam Quin's profile photo
5 comments
 
I'm now curious, why is it so?
 
I'm curious too. It's a confusing thing about the language, in my opinion, because it seems kind of natural or intuitive to me that these words would just be syntactic aliases for the traditional operators. This is probably why I got away with writing Ruby code for so long without realizing that there was a difference.. most of the time they appear to behave the same way.
 
Thanks +Mike Vincent! That makes sense, and in fact I do use these operators for flow control normally. I kind of go back and forth on using them as normal boolean operators, but now I'll be more consistent in how I use them.
 
Perl made the same decision, years ago, fwiw ☺