english and symbolic boolean operators in ruby

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

Upload: donschado

Post on 03-Jul-2015

172 views

Category:

Technology


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: English and symbolic boolean operators in Ruby

(and  &&)      and operators(or  ||)

@DonSchado | 15.01.2014

Page 2: English and symbolic boolean operators in Ruby

(and  &&)      and operators(or  ||)english and symbolic boolean

@DonSchado | 15.01.2014

Page 3: English and symbolic boolean operators in Ruby

Quiztime!!! \o/

Page 4: English and symbolic boolean operators in Ruby

foo  =  true  &&  "bar"

foo  =  ?

#=>  ?

Page 5: English and symbolic boolean operators in Ruby

foo  =  "bar"

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

Page 6: English and symbolic boolean operators in Ruby

foo  =  "bar"

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

foo  =  true  and  "bar"

foo  =  ?

#=>  ?

Page 7: English and symbolic boolean operators in Ruby

foo  =  "bar"

foo  =  true

#=>  "bar"

#=>  "bar"

foo  =  true  &&  "bar"

foo  =  true  and  "bar"

Page 8: English and symbolic boolean operators in Ruby

foo  =  42  &&  foo  *  2

foo  =  ?

#=>  ?

Page 9: English and symbolic boolean operators in Ruby

foo  =  42  &&  foo  *  2

foo  =  nil

#=>  NoMethodError:  undefined  method  `*'  for  nil:NilClass

Page 10: English and symbolic boolean operators in Ruby

foo  =  42  and  foo  *  2

foo  =  ?

#=>  ?

foo  =  42  &&  foo  *  2

foo  =  nil

#=>  NoMethodError:  undefined  method  `*'  for  nil:NilClass

Page 11: English and symbolic boolean operators in Ruby

foo  =  42  and  foo  *  2

foo  =  42

#=>  84

foo  =  42  &&  foo  *  2

foo  =  nil

#=>  NoMethodError:  undefined  method  `*'  for  nil:NilClass

Page 12: English and symbolic boolean operators in Ruby

short-circuit evaluation & precedence

Page 13: English and symbolic boolean operators in Ruby

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

Page 14: English and symbolic boolean operators in Ruby

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

Page 15: English and symbolic boolean operators in Ruby

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  :barend

#=>  :bar

Page 16: English and symbolic boolean operators in Ruby

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  :barend

#=>  :bar

:foo  ||  :bar #=>  :foo

Page 17: English and symbolic boolean operators in Ruby

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  :barend

#=>  :bar

:foo  ||  :bar

if  :foo  :fooelse  :barend

#=>  :foo

Page 18: English and symbolic boolean operators in Ruby

precedenceHigh precedence operations happen before low precedence operations.

!, ~, unary +**unary -*, /, %+, -<<, >>&|, ^>, >=, <, <=<=>, ==, ===, !=, =~, !~&&||.., ...?, :modifier-rescue=, +=, -=, etc.defined?notor, andmodifier-if, *-unless, *-while, *-until{ } blocks

http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html

high

low

Page 19: English and symbolic boolean operators in Ruby

precedenceHigh precedence operations happen before low precedence operations.

!, ~, unary +**unary -*, /, %+, -<<, >>&|, ^>, >=, <, <=<=>, ==, ===, !=, =~, !~&&||.., ...?, :modifier-rescue=, +=, -=, etc.defined?notor, andmodifier-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

=

Page 20: English and symbolic boolean operators in Ruby

precedenceHigh precedence operations happen before low precedence operations.

!, ~, unary +**unary -*, /, %+, -<<, >>&|, ^>, >=, <, <=<=>, ==, ===, !=, =~, !~&&||.., ...?, :modifier-rescue=, +=, -=, etc.defined?notor, andmodifier-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"

Page 21: English and symbolic boolean operators in Ruby

precedenceHigh precedence operations happen before low precedence operations.

!, ~, unary +**unary -*, /, %+, -<<, >>&|, ^>, >=, <, <=<=>, ==, ===, !=, =~, !~&&||.., ...?, :modifier-rescue=, +=, -=, etc.defined?notor, andmodifier-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))

Page 24: English and symbolic boolean operators in Ruby

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

Page 25: English and symbolic boolean operators in Ruby

raise  "Not  ready!"  unless  ready_to_rock?

examples

Page 26: English and symbolic boolean operators in Ruby

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

examples

Page 27: English and symbolic boolean operators in Ruby

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

examples

next  if  widget  =  widgets.pop

Page 28: English and symbolic boolean operators in Ruby

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

Page 29: English and symbolic boolean operators in Ruby

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

Page 30: English and symbolic boolean operators in Ruby

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