Upgrade to Pro — share decks privately, control downloads, hide ads and more …

English and symbolic boolean operators in Ruby

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.

DonSchado

January 15, 2014
Tweet

More Decks by DonSchado

Other Decks in Programming

Transcript

  1. (and  &&)       and operators (or  ||) english

    and symbolic boolean @DonSchado | 15.01.2014
  2. foo  =  "bar" #=>  "bar" foo  =  true  &&  "bar"

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

    foo  =  true  &&  "bar" foo  =  true  and  "bar"
  4. foo  =  42  &&  foo  *  2 foo  =  nil

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

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

    #=>  84 foo  =  42  &&  foo  *  2 foo  =  nil #=>   NoMethodError:   undefined  method  `*'  for  nil:NilClass
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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 =
  14. 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"
  15. 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))
  16. 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
  17. 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 =>
  18. @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 =>
  19. @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 =>