English and symbolic boolean operators in Ruby

5b7ac75124cebf11ff0de894b365198e?s=47 DonSchado
January 15, 2014

English and symbolic boolean operators in Ruby

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

5b7ac75124cebf11ff0de894b365198e?s=128

DonSchado

January 15, 2014
Tweet

Transcript

  1. (and  &&)       and operators (or  ||) @DonSchado

    | 15.01.2014
  2. (and  &&)       and operators (or  ||) english

    and symbolic boolean @DonSchado | 15.01.2014
  3. Quiztime!!! \o/

  4. foo  =  true  &&  "bar" foo  =  ? #=>  ?

  5. foo  =  "bar" #=>  "bar" foo  =  true  &&  "bar"

  6. foo  =  "bar" #=>  "bar" foo  =  true  &&  "bar"

    foo  =  true  and  "bar" foo  =  ? #=>  ?
  7. foo  =  "bar" foo  =  true #=>  "bar" #=>  "bar"

    foo  =  true  &&  "bar" foo  =  true  and  "bar"
  8. foo  =  42  &&  foo  *  2 foo  =  ?

    #=>  ?
  9. foo  =  42  &&  foo  *  2 foo  =  nil

    #=>   NoMethodError:   undefined  method  `*'  for  nil:NilClass
  10. foo  =  42  and  foo  *  2 foo  =  ?

    #=>  ? foo  =  42  &&  foo  *  2 foo  =  nil #=>   NoMethodError:   undefined  method  `*'  for  nil:NilClass
  11. foo  =  42  and  foo  *  2 foo  =  42

    #=>  84 foo  =  42  &&  foo  *  2 foo  =  nil #=>   NoMethodError:   undefined  method  `*'  for  nil:NilClass
  12. short-circuit evaluation & precedence

  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. 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. 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. 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 :foo  ||  :bar #=>  :foo
  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 if  :foo  :bar end #=>  :bar :foo  ||  :bar if  :foo  :foo else  :bar end #=>  :foo
  18. precedence High precedence operations happen before low precedence operations. !,

    ~, unary + ** unary - *, /, % +, - <<, >> & |, ^ >, >=, <, <= <=>, ==, ===, !=, =~, !~ && || .., ... ?, : modifier-rescue =, +=, -=, etc. defined? not or, and modifier-if, *-unless, *-while, *-until { } blocks http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html high low
  19. precedence High precedence operations happen before low precedence operations. !,

    ~, unary + ** unary - *, /, % +, - <<, >> & |, ^ >, >=, <, <= <=>, ==, ===, !=, =~, !~ && || .., ... ?, : modifier-rescue =, +=, -=, etc. defined? not or, and modifier-if, *-unless, *-while, *-until { } blocks http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html high low ≠ just to make life more interesting =
  20. precedence High precedence operations happen before low precedence operations. !,

    ~, unary + ** unary - *, /, % +, - <<, >> & |, ^ >, >=, <, <= <=>, ==, ===, !=, =~, !~ && || .., ... ?, : modifier-rescue =, +=, -=, etc. defined? not or, and modifier-if, *-unless, *-while, *-until { } blocks http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html high low ≠ just to make life more interesting = foo  =  (true  &&  "bar") (foo  =  true)  and  "bar"
  21. precedence High precedence operations happen before low precedence operations. !,

    ~, unary + ** unary - *, /, % +, - <<, >> & |, ^ >, >=, <, <= <=>, ==, ===, !=, =~, !~ && || .., ... ?, : modifier-rescue =, +=, -=, etc. defined? not or, and modifier-if, *-unless, *-while, *-until { } blocks http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html high low ≠ just to make life more interesting = foo  =  (true  &&  "bar") (foo  =  true)  and  "bar" (foo  =  42)  and  (foo  *  2) foo  =  (42  &&  (foo  *  2))
  22. http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-and

  23. http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-or

  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. raise  "Not  ready!"  unless  ready_to_rock? examples

  26. raise  "Not  ready!"  unless  ready_to_rock? ready_to_rock?  or  raise  "Not  ready!"

    => examples
  27. raise  "Not  ready!"  unless  ready_to_rock? ready_to_rock?  or  raise  "Not  ready!"

    => examples next  if  widget  =  widgets.pop
  28. raise  "Not  ready!"  unless  ready_to_rock? ready_to_rock?  or  raise  "Not  ready!"

    => examples next  if  widget  =  widgets.pop widget  =  widgets.pop  and  next =>
  29. @post  =  Post.find_by_name(name)  and  @post.publish! raise  "Not  ready!"  unless  ready_to_rock?

    ready_to_rock?  or  raise  "Not  ready!" => examples next  if  widget  =  widgets.pop widget  =  widgets.pop  and  next =>
  30. @post  =  Post.find_by_name(name)  and  @post.publish! foo  =  get_foo  or  raise

     "Could  not  find  foo!" raise  "Not  ready!"  unless  ready_to_rock? ready_to_rock?  or  raise  "Not  ready!" => examples next  if  widget  =  widgets.pop widget  =  widgets.pop  and  next =>
  31. 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://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html 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