Esteban Pastorino

Quick Ruby Tip: || vs or

Quick heads up on operator precedence:

If you want to assign a value to a variable depending on a boolean, use || not or.

|| takes precendence over =. or does not.

Example:

or operator
1
2
a = false or true
puts a # => false
|| operator
1
2
a = false || true
puts a # => true

I got stuck a few minutes today until I realized where the error was.

Hope this helps and you don’t fall in the same trap :)

Comments