english and symbolic boolean operators in ruby

Post on 03-Jul-2015

172 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

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

@DonSchado | 15.01.2014

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

@DonSchado | 15.01.2014

Quiztime!!! \o/

foo  =  true  &&  "bar"

foo  =  ?

#=>  ?

foo  =  "bar"

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

foo  =  "bar"

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

foo  =  true  and  "bar"

foo  =  ?

#=>  ?

foo  =  "bar"

foo  =  true

#=>  "bar"

#=>  "bar"

foo  =  true  &&  "bar"

foo  =  true  and  "bar"

foo  =  42  &&  foo  *  2

foo  =  ?

#=>  ?

foo  =  42  &&  foo  *  2

foo  =  nil

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

foo  =  42  and  foo  *  2

foo  =  ?

#=>  ?

foo  =  42  &&  foo  *  2

foo  =  nil

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

foo  =  42  and  foo  *  2

foo  =  42

#=>  84

foo  =  42  &&  foo  *  2

foo  =  nil

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

short-circuit evaluation & precedence

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

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

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

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

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

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

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

=

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"

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

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

raise  "Not  ready!"  unless  ready_to_rock?

examples

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

examples

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

examples

next  if  widget  =  widgets.pop

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

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

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

top related