$30 off During Our Annual Pro Sale. View Details »

English and symbolic boolean operators in Ruby

DonSchado
January 15, 2014

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
Tweet

More Decks by DonSchado

Other Decks in Programming

Transcript

  1. (and  &&)    
     
    and operators
    (or  ||)
    @DonSchado | 15.01.2014

    View Slide

  2. (and  &&)    
     
    and operators
    (or  ||)
    english and symbolic boolean
    @DonSchado | 15.01.2014

    View Slide

  3. Quiztime!!! \o/

    View Slide

  4. foo  =  true  &&  "bar"
    foo  =  ?
    #=>  ?

    View Slide

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

    View Slide

  6. foo  =  "bar"
    #=>  "bar"
    foo  =  true  &&  "bar"
    foo  =  true  and  "bar"
    foo  =  ?
    #=>  ?

    View Slide

  7. foo  =  "bar"
    foo  =  true
    #=>  "bar"
    #=>  "bar"
    foo  =  true  &&  "bar"
    foo  =  true  and  "bar"

    View Slide

  8. foo  =  42  &&  foo  *  2
    foo  =  ?
    #=>  ?

    View Slide

  9. foo  =  42  &&  foo  *  2
    foo  =  nil
    #=>  
    NoMethodError:  
    undefined  method  `*'  for  nil:NilClass

    View Slide

  10. foo  =  42  and  foo  *  2
    foo  =  ?
    #=>  ?
    foo  =  42  &&  foo  *  2
    foo  =  nil
    #=>  
    NoMethodError:  
    undefined  method  `*'  for  nil:NilClass

    View Slide

  11. foo  =  42  and  foo  *  2
    foo  =  42
    #=>  84
    foo  =  42  &&  foo  *  2
    foo  =  nil
    #=>  
    NoMethodError:  
    undefined  method  `*'  for  nil:NilClass

    View Slide

  12. short-circuit evaluation & precedence

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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
    if  :foo
     :bar
    end
    #=>  :bar
    :foo  ||  :bar #=>  :foo

    View Slide

  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
    if  :foo
     :bar
    end
    #=>  :bar
    :foo  ||  :bar
    if  :foo
     :foo
    else
     :bar
    end
    #=>  :foo

    View Slide

  18. 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

    View Slide

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

    View Slide

  20. 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"

    View Slide

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

    View Slide

  22. http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-and

    View Slide

  23. http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-or

    View Slide

  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

    View Slide

  25. raise  "Not  ready!"  unless  ready_to_rock?
    examples

    View Slide

  26. raise  "Not  ready!"  unless  ready_to_rock? ready_to_rock?  or  raise  "Not  ready!"
    =>
    examples

    View Slide

  27. raise  "Not  ready!"  unless  ready_to_rock? ready_to_rock?  or  raise  "Not  ready!"
    =>
    examples
    next  if  widget  =  widgets.pop

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  31. 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

    View Slide