Good Ruby Idiom: and/or operators

10-Sep-2016 4177

Any tutorial and book will teach you there are two sets of similar operators in Ruby: &&/|| vsand/or (and also &/| for bit operations, but that’s not the case today). But typical tutorial will not provide further explanation why we need both of those pairs.

Some of tutorials and blog posts about this matter even go as far as explicit recommendations never useand/or, because && and || is enough for all reasonable cases, and because and/or provide only confusion. They would typically warn you about and’s lower precedence than =, but nobody will tell you how this can and should be useful.

Use coupon code:

RUBYONRAILS

to get 30% discount on our bundle!
Prepare for your next tech interview with our comprehensive collection of programming interview guides. Covering JavaScript, Ruby on Rails, React, and Python, these highly-rated books offer thousands of essential questions and answers to boost your interview success. Buy our 'Ultimate Job Interview Preparation eBook Bundle' featuring 2200+ questions across multiple languages. Ultimate Job Interview Preparation eBook Bundle