13. 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
14. 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
15. 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
16. 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
if
:foo
:bar
end
:foo
||
:bar
#=>
:foo
17. 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
if
:foo
:bar
end
:foo
||
:bar
if
:foo
:foo
else
:bar
end
#=>
:foo
18. precedence
high
!, ~, unary +
**
unary *, /, %
+, <<, >>
&
|, ^
>, >=, <, <=
<=>, ==, ===, !=, =~, !~
&&
||
.., ...
?, :
modifier-rescue
=, +=, -=, etc.
defined?
not
or, and
modifier-if, *-unless, *-while, *-until
{ } blocks
High precedence operations happen
before low precedence operations.
http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html
low
19. precedence
high
!, ~, unary +
**
unary *, /, %
+, <<, >>
&
|, ^
>, >=, <, <=
<=>, ==, ===, !=, =~, !~
&&
||
.., ...
?, :
modifier-rescue
=, +=, -=, etc.
defined?
not
or, and
modifier-if, *-unless, *-while, *-until
{ } blocks
High precedence operations happen
before low precedence operations.
≠ just to make life
more interesting
=
http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html
low
20. precedence
high
!, ~, unary +
**
unary *, /, %
+, <<, >>
&
|, ^
>, >=, <, <=
<=>, ==, ===, !=, =~, !~
&&
||
.., ...
?, :
modifier-rescue
=, +=, -=, etc.
defined?
not
or, and
modifier-if, *-unless, *-while, *-until
{ } blocks
High precedence operations happen
before low precedence operations.
foo
=
(true
&&
"bar")
(foo
=
true)
and
"bar"
≠ just to make life
more interesting
=
http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html
low
21. precedence
high
!, ~, unary +
**
unary *, /, %
+, <<, >>
&
|, ^
>, >=, <, <=
<=>, ==, ===, !=, =~, !~
&&
||
.., ...
?, :
modifier-rescue
=, +=, -=, etc.
defined?
not
or, and
modifier-if, *-unless, *-while, *-until
{ } blocks
High precedence operations happen
before low precedence operations.
foo
=
(true
&&
"bar")
(foo
=
true)
and
"bar"
≠ just to make life
more interesting
foo
=
(42
&&
(foo
*
2))
(foo
=
42)
and
(foo
*
2)
=
http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html
low
24. 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
27. examples
raise
"Not
ready!"
unless
ready_to_rock?
next
if
widget
=
widgets.pop
=>
ready_to_rock?
or
raise
"Not
ready!"
28. examples
raise
"Not
ready!"
unless
ready_to_rock?
=>
ready_to_rock?
or
raise
"Not
ready!"
next
if
widget
=
widgets.pop
=>
widget
=
widgets.pop
and
next
29. examples
raise
"Not
ready!"
unless
ready_to_rock?
=>
ready_to_rock?
or
raise
"Not
ready!"
next
if
widget
=
widgets.pop
=>
widget
=
widgets.pop
and
next
@post
=
Post.find_by_name(name)
and
@post.publish!
30. examples
raise
"Not
ready!"
unless
ready_to_rock?
=>
ready_to_rock?
or
raise
"Not
ready!"
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!"