# Boolean Operators

**URL:** <https://www.ruby-forum.com/t/boolean-operators/133223>\
**Category:** Ruby\
**Created:** [March 29, 2008, 12:21am UTC](https://www.ruby-forum.com/t/boolean-operators/133223 "2008-03-29T00:21:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [March 29, 2008, 12:21am UTC](https://www.ruby-forum.com/t/boolean-operators/133223/1 "2008-03-29T00:21:42Z")

</div>

Quick question:

I know that the ‘and’ and ‘or’ operators in ruby have the same  
precedence, and ‘&&’ has a higher one than ‘||’ - but is there a best  
practise for which one to use when precedence isn’t important?

Thanks,

~ Mark

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [March 29, 2008, 6:58am UTC](https://www.ruby-forum.com/t/boolean-operators/133223/2 "2008-03-29T06:58:49Z")

</div>

On Sat, Mar 29, 2008 at 08:22:00AM +0900, Mark D. wrote:

> Quick question:
> 
> I know that the ‘and’ and ‘or’ operators in ruby have the same  
> precedence, and ‘&&’ has a higher one than ‘||’ - but is there a best  
> practise for which one to use when precedence isn’t important?

I think “best practice” in this case is to use whatever makes it most  
readable. I tend to like “and” and “or” (except when I don’t). Notable  
exceptions include operators like ||= and &&=, which don’t use “and” and  
“or”.

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [March 29, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/boolean-operators/133223/3 "2008-03-29T18:00:54Z")

</div>

On 29.03.2008 00:22, Mark D. wrote:

> I know that the ‘and’ and ‘or’ operators in ruby have the same  
> precedence, and ‘&&’ has a higher one than ‘||’ - but is there a best  
> practise for which one to use when precedence isn’t important?

I usually use && and || because they visually stand out better than  
“and” and “or” and can be used with assignments (higher precedence than  
“=”). I use “and” and “or” rather seldom mostly when chaining commands  
as in

foo = doit() and puts foo

YMMV

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [March 30, 2008, 2:17pm UTC](https://www.ruby-forum.com/t/boolean-operators/133223/4 "2008-03-30T14:17:26Z")

</div>

On 3/29/08, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I use “and” and “or” rather seldom mostly when chaining commands  
> as in
> 
> foo = doit() and puts foo

Keeping in mind that using && instead of and here has quite a  
different result, even after adjusting the syntax of the puts call:

irb(main):001:0\> a = 1 and puts “foo”  
foo  
=\> nil  
irb(main):002:0\> a  
=\> 1  
irb(main):003:0\> a = 1 && puts “foo”  
SyntaxError: compile error  
(irb):3: syntax error, unexpected tSTRING\_BEG, expecting kDO or ‘{’ or  
‘(’  
a = 1 && puts “foo”  
^  
from (irb):3  
irb(main):004:0\> a = 1 && puts(“foo”)  
foo  
=\> nil  
irb(main):005:0\> a  
=\> nil

I don’t know that I’ve ever used ‘and’ since being bitten by the  
relative precedence of = and ‘and’ in a rails app I inherited and was  
extending.

I also don’t believe that I’d personally use  
a = 1 and puts “foo”

rather than splitting it into two lines, perhaps replacing the and  
with a semi-colon or otherwise steppng back and micro-refactoring the  
code to make it clearer.

## But I could be wrong.

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [March 30, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/boolean-operators/133223/5 "2008-03-30T14:35:53Z")

</div>

On 30.03.2008 14:16, Rick DeNatale wrote:

> On 3/29/08, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I use “and” and “or” rather seldom mostly when chaining commands  
> > as in
> > 
> > foo = doit() and puts foo
> 
> Keeping in mind that using && instead of and here has quite a  
> different result, even after adjusting the syntax of the puts call:

Which is the precise reason why I would use “and” in this case. Note  
that I wrote “seldom” so this is not a too frequent idiom I employ.

> rather than splitting it into two lines, perhaps replacing the and  
> with a semi-colon or otherwise steppng back and micro-refactoring the  
> code to make it clearer.
> 
> But I could be wrong.

I don’t think so. This is also a question of personal style and I don’t  
find anything wrong with your approach.

Kind regards

robert
