Ruby - Boolean Operators mixture

Introduction

Ruby's Boolean operators can sometimes behave in an unpredictable manner. For example:

puts( (not( 1==1 )) )                # This is okay 
puts( not( 1==1 ) )                  # Syntax error in Ruby 1.8 
                                     # but okay in Ruby 1.9 

puts( true && true && !(true) )      # This is okay 
puts( true && true and !(true) )     # This is a syntax error 

puts( ((true) and (true)) )          # This is okay 
puts( true && true )                 # This is okay 
puts( true and true )                # This is a syntax error 

You can avoid problems by sticking to one style of operator (either and, or, and not or &&, ||, and !) rather than mixing the two.

In addition, the use of parentheses is recommended!

Related Topic