Share
Embed
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
Transcript
31 slides
-
(and &&) and operators (or ||) @DonSchado
| 15.01.2014 -
(and &&) and operators (or ||) english
and symbolic boolean @DonSchado | 15.01.2014 -
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 = 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 (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 :bar end #=> :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 :bar end #=> :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 :bar end #=> :bar :foo || :bar if :foo :foo else :bar end #=> :foo -
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 -
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 = -
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" -
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)) -
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? 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 =>