Difference between && and (and) in Ruby

Till the date I used to believe that in ruby && and (and) are completely replaceable but in actual its not the case at all.

Difference

Precedence of the operators is point to note/remember here.

&& has a higher precedence over = and (and). while (and) has a lower precedence over = and &&.

  a = true and false #(Here a = true will be evaluated first and and the value of a becomes true.)
  a = true && false  #(Here true && false will be evaluated first and the value of a becomes false.)

Same is the case with || and or.

comments powered by Disqus