Ruby, && & and, || & or

I keep encountering it in many places. So here’s a short guide about Ruby logical operators.

The essential difference between perl-ish (&& and ||) and human-ish (and and or) versions of this operators is priority.

Quick example:

1
2true or false and false # => false
true || false && false #=> true

This happens because && has higher priority than ||, but and is the same priority as or.

Here’s excerpt from Ruby Operator Precenedce Table listing operators from highest precedence to lowest:

Also nothe that and and or have even lower priority than assignment operators.

Know your language.

Sat, 29 May, 2010, 12:35
Tags: