Created
November 24, 2010 21:21
-
-
Save fredlee/714442 to your computer and use it in GitHub Desktop.
'and' vs. '&&'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This is a contrived example. | |
| # But, this simple example demonstrates some code I have seen where not understanding the difference | |
| # between 'and' and '&&' has caused unexpected behavior. | |
| # The intent is to evaluate the 3rd clause if the 1st and 2nd clause is true. | |
| # Again, this is a contrived example. | |
| # But, imagine that the 1st and 2nd clause is a method invocation or something like that. | |
| # This is a classic example where the goal of terseness really caused problems. | |
| >> x = 10 && y = 5 && x - y | |
| NoMethodError: undefined method `-' for nil:NilClass | |
| from (irb):1 | |
| from /Users/flee1/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>' | |
| >> x = 10 and y = 5 and x - y | |
| => 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment