Alex Baranosky Alex Baranosky - 4 months ago 4x
Ruby Question

Difference between "or" and || in Ruby?

What's the difference between the

operators in Ruby? Or is it just preference?


It's a matter of operator precedence.

|| has a higher precedence than or.

So, in between the two you have other operators including ternary (? :) and assignment (=) so which one you choose can affect the outcome of statements.

Here's a ruby operator precedence table.

See this question for another example using and/&&.

Also, be aware of some nasty things that could happen: a = false or true sets a to false, and a = false || true sets a to true, since = precedence is lower than || but higher than or.