Simon Rentzke

Blue Blistering Barnacles

Ruby "&& vs and" , "or vs ||"

tl;dr && and || have higher precendence

Difference between 'and' and '&&'

cat = 'meow'
dog = false


answer = cat && dog
=> false

answer
=>  false

answer = cat and dog
=> false

answer
=> 'meow'

Similarly with 'or' and '||'

cat = false
dog = 'bark'

answer = cat || dog
 => 'bark'

answer
=>  'bark'

answer = cat or dog
=> 'bark'

answer
=> false

I generally follow this style guide

Back to Blog