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