Skip to content

Instantly share code, notes, and snippets.

@fredlee
Created November 24, 2010 21:21
Show Gist options
  • Select an option

  • Save fredlee/714442 to your computer and use it in GitHub Desktop.

Select an option

Save fredlee/714442 to your computer and use it in GitHub Desktop.
'and' vs. '&&'
# 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