English and symbolic boolean operators in Ruby

  • View
    164

  • Download
    1

Embed Size (px)

DESCRIPTION

learn the difference between the boolean operators and what this has to do with short-circuit evaluation and precedence.

Text of English and symbolic boolean operators in Ruby

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

2. and (and&&) (or||) operators olic boolean nglish and symb e@DonSchado | 15.01.2014 3. Quiztime!!! o/ 4. foo=true&&"bar" foo=?#=>? 5. foo=true&&"bar" foo="bar"#=>"bar" 6. foo=true&&"bar"#=>"bar"foo="bar"foo=trueand"bar" foo=?#=>? 7. foo=true&&"bar"#=>"bar"foo="bar"foo=trueand"bar" foo=true#=>"bar" 8. foo=42&&foo*2 foo=?#=>? 9. foo=42&&foo*2#=>NoMethodError: undefinedmethod`*'fornil:NilClassfoo=nil 10. foo=42&&foo*2#=>NoMethodError: undefinedmethod`*'fornil:NilClassfoo=nilfoo=42andfoo*2 foo=?#=>? 11. foo=42&&foo*2#=>NoMethodError: undefinedmethod`*'fornil:NilClassfoo=nilfoo=42andfoo*2 foo=42#=>84 12. short-circuit evaluation & precedence 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 rst argument does not sufce 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 rst argument does not sufce 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 rst argument does not sufce 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 rst argument does not sufce 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#=>:barif: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 rst argument does not sufce 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#=>:barif:foo :bar end:foo||:bar if:foo :foo else :bar end#=>:foo 18. precedencehigh!, ~, unary + ** unary *, /, % +, & |, ^ >, >=, =, =, =, ready_to_rock?orraise"Notready!" 28. examples raise"Notready!"unlessready_to_rock?=>ready_to_rock?orraise"Notready!"nextifwidget=widgets.pop=>widget=widgets.popandnext 29. examples raise"Notready!"unlessready_to_rock?=>ready_to_rock?orraise"Notready!"nextifwidget=widgets.pop=>widget=widgets.popandnext@post=Post.find_by_name(name)and@post.publish! 30. examples raise"Notready!"unlessready_to_rock?=>ready_to_rock?orraise"Notready!"nextifwidget=widgets.pop=>widget=widgets.popandnext@post=Post.find_by_name(name)and@post.publish!foo=get_fooorraise"Couldnotfindfoo!" 31. http://www.ruby-doc.org/core-2.0.0/doc/syntax/precedence_rdoc.html 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-demystied-and-vs 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