Slide 1

Slide 1 text

(and  &&)       and operators (or  ||) @DonSchado | 15.01.2014

Slide 2

Slide 2 text

(and  &&)       and operators (or  ||) english and symbolic boolean @DonSchado | 15.01.2014

Slide 3

Slide 3 text

Quiztime!!! \o/

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

foo  =  "bar" #=>  "bar" foo  =  true  &&  "bar" foo  =  true  and  "bar" foo  =  ? #=>  ?

Slide 7

Slide 7 text

foo  =  "bar" foo  =  true #=>  "bar" #=>  "bar" foo  =  true  &&  "bar" foo  =  true  and  "bar"

Slide 8

Slide 8 text

foo  =  42  &&  foo  *  2 foo  =  ? #=>  ?

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

short-circuit evaluation & precedence

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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 =

Slide 20

Slide 20 text

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"

Slide 21

Slide 21 text

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))

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

raise  "Not  ready!"  unless  ready_to_rock? examples

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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 =>

Slide 29

Slide 29 text

@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 =>

Slide 30

Slide 30 text

@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 =>

Slide 31

Slide 31 text

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