Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

English and symbolic boolean operators in Ruby

514 views

Published on

learn the difference between the boolean operators and what
this has to do with short-circuit evaluation and precedence.

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

English and symbolic boolean operators in Ruby

  1. 1. and (and  &&)     (or  ||) operators   @DonSchado | 15.01.2014
  2. 2. and (and  &&)     (or  ||) operators   olic boolean nglish and symb e @DonSchado | 15.01.2014
  3. 3. Quiztime!!! o/
  4. 4. foo  =  true  &&  "bar" foo  =  ? #=>  ?
  5. 5. foo  =  true  &&  "bar" foo  =  "bar" #=>  "bar"
  6. 6. foo  =  true  &&  "bar" #=>  "bar" foo  =  "bar" foo  =  true  and  "bar" foo  =  ? #=>  ?
  7. 7. foo  =  true  &&  "bar" #=>  "bar" foo  =  "bar" foo  =  true  and  "bar" foo  =  true #=>  "bar"
  8. 8. foo  =  42  &&  foo  *  2 foo  =  ? #=>  ?
  9. 9. foo  =  42  &&  foo  *  2 #=>  NoMethodError:   undefined  method  `*'  for  nil:NilClass foo  =  nil
  10. 10. foo  =  42  &&  foo  *  2 #=>  NoMethodError:   undefined  method  `*'  for  nil:NilClass foo  =  nil foo  =  42  and  foo  *  2 foo  =  ? #=>  ?
  11. 11. foo  =  42  &&  foo  *  2 #=>  NoMethodError:   undefined  method  `*'  for  nil:NilClass foo  =  nil foo  =  42  and  foo  *  2 foo  =  42 #=>  84
  12. 12. short-circuit evaluation & precedence
  13. 13. short-circuit evaluation (or McCarthy evaluation) ”... denotes the semantics of some Boolean operators [...] in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression.” ”Short-circuit operators are, in effect, control structures rather than simple arithmetic operators” http://en.wikipedia.org/wiki/Short-circuit_evaluation
  14. 14. short-circuit evaluation (or McCarthy evaluation) ”... denotes the semantics of some Boolean operators [...] in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression.” ”Short-circuit operators are, in effect, control structures rather than simple arithmetic operators” http://en.wikipedia.org/wiki/Short-circuit_evaluation :foo  &&  :bar #=>  :bar
  15. 15. short-circuit evaluation (or McCarthy evaluation) ”... denotes the semantics of some Boolean operators [...] in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression.” ”Short-circuit operators are, in effect, control structures rather than simple arithmetic operators” http://en.wikipedia.org/wiki/Short-circuit_evaluation :foo  &&  :bar if  :foo  :bar end #=>  :bar
  16. 16. short-circuit evaluation (or McCarthy evaluation) ”... denotes the semantics of some Boolean operators [...] in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression.” ”Short-circuit operators are, in effect, control structures rather than simple arithmetic operators” http://en.wikipedia.org/wiki/Short-circuit_evaluation :foo  &&  :bar #=>  :bar if  :foo  :bar end :foo  ||  :bar #=>  :foo
  17. 17. short-circuit evaluation (or McCarthy evaluation) ”... denotes the semantics of some Boolean operators [...] in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression.” ”Short-circuit operators are, in effect, control structures rather than simple arithmetic operators” http://en.wikipedia.org/wiki/Short-circuit_evaluation :foo  &&  :bar #=>  :bar if  :foo  :bar end :foo  ||  :bar if  :foo  :foo else  :bar end #=>  :foo
  18. 18. precedence high !, ~, unary + ** unary *, /, % +, <<, >> & |, ^ >, >=, <, <= <=>, ==, ===, !=, =~, !~ && || .., ... ?, : modifier-rescue =, +=, -=, etc. defined? not or, and modifier-if, *-unless, *-while, *-until { } blocks High precedence operations happen before low precedence operations. http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html low
  19. 19. precedence high !, ~, unary + ** unary *, /, % +, <<, >> & |, ^ >, >=, <, <= <=>, ==, ===, !=, =~, !~ && || .., ... ?, : modifier-rescue =, +=, -=, etc. defined? not or, and modifier-if, *-unless, *-while, *-until { } blocks High precedence operations happen before low precedence operations. ≠ just to make life more interesting = http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html low
  20. 20. precedence high !, ~, unary + ** unary *, /, % +, <<, >> & |, ^ >, >=, <, <= <=>, ==, ===, !=, =~, !~ && || .., ... ?, : modifier-rescue =, +=, -=, etc. defined? not or, and modifier-if, *-unless, *-while, *-until { } blocks High precedence operations happen before low precedence operations. foo  =  (true  &&  "bar") (foo  =  true)  and  "bar" ≠ just to make life more interesting = http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html low
  21. 21. precedence high !, ~, unary + ** unary *, /, % +, <<, >> & |, ^ >, >=, <, <= <=>, ==, ===, !=, =~, !~ && || .., ... ?, : modifier-rescue =, +=, -=, etc. defined? not or, and modifier-if, *-unless, *-while, *-until { } blocks High precedence operations happen before low precedence operations. foo  =  (true  &&  "bar") (foo  =  true)  and  "bar" ≠ just to make life more interesting foo  =  (42  &&  (foo  *  2)) (foo  =  42)  and  (foo  *  2) = http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html low
  22. 22. http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-and
  23. 23. http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-or
  24. 24. don‘t be afraid of and and or • not be suitable for regular boolean logic • use them for readable control-flow scenarios • don‘t mix them up with && and || in the same expression
  25. 25. examples raise  "Not  ready!"  unless  ready_to_rock?
  26. 26. examples raise  "Not  ready!"  unless  ready_to_rock? => ready_to_rock?  or  raise  "Not  ready!"
  27. 27. examples raise  "Not  ready!"  unless  ready_to_rock? next  if  widget  =  widgets.pop => ready_to_rock?  or  raise  "Not  ready!"
  28. 28. examples raise  "Not  ready!"  unless  ready_to_rock? => ready_to_rock?  or  raise  "Not  ready!" next  if  widget  =  widgets.pop => widget  =  widgets.pop  and  next
  29. 29. examples raise  "Not  ready!"  unless  ready_to_rock? => ready_to_rock?  or  raise  "Not  ready!" next  if  widget  =  widgets.pop => widget  =  widgets.pop  and  next @post  =  Post.find_by_name(name)  and  @post.publish!
  30. 30. examples raise  "Not  ready!"  unless  ready_to_rock? => ready_to_rock?  or  raise  "Not  ready!" next  if  widget  =  widgets.pop => widget  =  widgets.pop  and  next @post  =  Post.find_by_name(name)  and  @post.publish! foo  =  get_foo  or  raise  "Could  not  find  foo!"
  31. 31. http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/ http://ruby-doc.com/docs/ProgrammingRuby/html/tut_expressions.html#UG https://www.tinfoilsecurity.com/blog/ruby-demystified-and-vs http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-and http://blog.revathskumar.com/2013/05/short-circuit-evaluation-in-ruby.html

×