English and symbolic boolean operators in Ruby
-
Upload
donschado -
Category
Technology
-
view
174 -
download
1
description
Transcript of English and symbolic boolean operators in Ruby
![Page 1: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/1.jpg)
(and &&) and operators(or ||)
@DonSchado | 15.01.2014
![Page 2: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/2.jpg)
(and &&) and operators(or ||)english and symbolic boolean
@DonSchado | 15.01.2014
![Page 3: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/3.jpg)
Quiztime!!! \o/
![Page 4: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/4.jpg)
foo = true && "bar"
foo = ?
#=> ?
![Page 5: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/5.jpg)
foo = "bar"
#=> "bar"foo = true && "bar"
![Page 6: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/6.jpg)
foo = "bar"
#=> "bar"foo = true && "bar"
foo = true and "bar"
foo = ?
#=> ?
![Page 7: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/7.jpg)
foo = "bar"
foo = true
#=> "bar"
#=> "bar"
foo = true && "bar"
foo = true and "bar"
![Page 8: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/8.jpg)
foo = 42 && foo * 2
foo = ?
#=> ?
![Page 9: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/9.jpg)
foo = 42 && foo * 2
foo = nil
#=> NoMethodError: undefined method `*' for nil:NilClass
![Page 10: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/10.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/11.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/12.jpg)
short-circuit evaluation & precedence
![Page 13: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/13.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/14.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/15.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/16.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/17.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/18.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/19.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/20.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/21.jpg)
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 22: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/22.jpg)
http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-and
![Page 23: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/23.jpg)
http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-or
![Page 24: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/24.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/25.jpg)
raise "Not ready!" unless ready_to_rock?
examples
![Page 26: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/26.jpg)
raise "Not ready!" unless ready_to_rock? ready_to_rock? or raise "Not ready!"=>
examples
![Page 27: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/27.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/28.jpg)
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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/29.jpg)
@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](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/30.jpg)
@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=>
![Page 31: English and symbolic boolean operators in Ruby](https://reader030.fdocuments.us/reader030/viewer/2022020307/55961aab1a28ab942d8b482e/html5/thumbnails/31.jpg)
http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/
http://ruby-doc.com/docs/ProgrammingRuby/html/tut_expressions.html#UG
https://www.tinfoilsecurity.com/blog/ruby-demystified-and-vs
http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html
http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-and
http://blog.revathskumar.com/2013/05/short-circuit-evaluation-in-ruby.html