Skip to content

Instantly share code, notes, and snippets.

@hannestyden
Created May 8, 2010 10:14
Show Gist options
  • Save hannestyden/394477 to your computer and use it in GitHub Desktop.
Save hannestyden/394477 to your computer and use it in GitHub Desktop.
# Logical operator precedence in Ruby
#
# Problems might arise in combination with the assignment operator.
#
# http://blog.jayfields.com/2007/08/ruby-operator-precedence-of-and-which.html
# http://phrogz.net/ProgrammingRuby/language.html#table_18.4
# Definitions:
# '&&' and '||' are logical operators
# 'and' and 'or' are logical composition operators
#
# '&&' compared to 'and'
#
a = true && false # => false
# \___________/
a # => false
b = true and false # => false
# \______/
b # => true (!)
c = (true and false) # => false
# \______________/
c # => false
#
# '||' compared to 'or'
#
d = false || true # => true
# \___________/
d # => true
e = false or true # => true
# \_______/
e # => false (!)
f = (false or true) # => true
# \_____________/
f # => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment